Results 1 to 2 of 2
  1. #1
    Join Date
    Jan 2009
    Posts
    13

    Unanswered: Getting Access Data into a Word Doc

    as of right now i am just testing the method before i go full scale into my real DB so i have created a testing table (tblCustomer) with 3 fields:
    *CustID (Autonumber, primary key)
    *CustName (Text)
    *Address (Text)

    I have created a test Word Doc called 'CustomerSlip.doc'. This document has a place for each field and is named exactly the same as my Test DB as shown in the directions found on this page How do I... Fill Word form fields with Access data? | Microsoft Office | TechRepublic.com

    When i run the following code, i get a "Run-time error '438': Object Doesn't support this property or method" on the line '.Visible = True'

    Some of the help i have looked at mentions it being a problem with some sort of ADO references being too new or to old or something. Not exactly sure how to figure out which one to use or what to do there so i was thinking that there might be a way around it. One example: [RESOLVED] Runtime error 438 Object doesn't support this property or method - VBForums

    Another thing i don't understand is that every other time or so that i click my button to run this code, it will work and pull up the document with the information in it but it still gives me the error code behind it.

    Here is my code. it is pretty much exactly what i got from the website mentioned above. This is the first option that takes the info directly from the form. i will eventually use the second option that pulls straight from the recordset but i wanted to get the simple one to work first:

    Code:
    Private Sub cmdRFP_Click()
    'Print customer slip for current customer.
    Dim appWord As Word.Application
    Dim doc As Word.Document
    
    'Avoid error 429, when Word isn't open.
    On Error Resume Next
    Err.Clear
    
    'Set appWord object variable to running instance of Word.
    Set appWord = GetObject(, "Word.Application")
    
    If Err.Number <> 0 Then
    'If Word isn't open, create a new instance of Word.
    Set appWord = New Word.Application
    End If
    
    Set doc = appWord.Documents.Open("C:\Documents and Settings\jloudermilk\Desktop\DB Test Docs\CustomerSlip.doc", , True)
    With doc
    .FormFields("CustID").Result = Me!CustID
    .FormFields("CustName").Result = Me!CustName
    .FormFields("Address").Result = Me!Address
    .Visible = True 'This is where my error occurs
    .Activate
    End With
    Set doc = Nothing
    Set appWord = Nothing
    Exit Sub
    errHandler:
    MsgBox Err.Number & ": " & Err.Description
    End Sub

  2. #2
    Join Date
    Jan 2009
    Posts
    13

    This is along the same lines but a little farther down the road

    This comes from the problem above but i have advanced a little way thru the problem now. here is my new situation

    i have this bit of code and it is supposed to go thru a form and grab each textbox name and adds "fld" before it and stores it in stCtrl1 and the "Me!" to the front and stores it as stCtrl2. it then plugs these two Strings into this command, .FormFields(stCtrl1).Result = stCtrl2, that transfers data from the Control on the form (stCtrl2) to the field on the word doc (stCtrl1). This is where my errors are occuring. if i hard code each control name (below my loop as comments) it works perfectly. however, when i try to automate the process, can't seem to get the syntax right. i have tried different combinations of variables, &'s, "fld" & stCtrl, etc with no luck. the code below results in the following error:

    Runtime error '5941': the requested member of the collection doesn't exist

    Thanks for your help in advance.

    Code:
    Private Sub cmdRFP_Click()
        
    On Error GoTo Err_OpenWord
    
        '--- opens a new Word Document
    Dim wordApp As Word.Application
    Dim wordDoc As Word.Document
    Set wordApp = New Word.Application
    With wordApp
        .Visible = True
        Set wordDoc = .Documents.Open("C:\Documents and Settings\jloudermilk\Desktop\DB Test Docs\RFP.dot", , False)
    End With
    
    Dim ctrl As Control
    Dim stCtrl1 As String
    Dim stCtrl2 As String
    
    
        
    
        With wordDoc
            For Each ctrl In Me.Controls
                If ctrl.ControlType = acTextBox Then
                    stCtrl1 = "fld" & ctrl.Name
                    stCtrl2 = "Me!" & ctrl.Name
                    
                    .FormFields(stCtrl1).Result = stCtrl2
                    'Debug.Print stCtrl1
                    'Debug.Print stCtrl2
                End If
            Next ctrl
            
    '        .FormFields("fldCustName").Result = Me!CustName
    '        .FormFields("fldAddress").Result = Me!Address
    '        .FormFields("fldCity").Result = Me!City
    '        .FormFields("fldState").Result = Me!State
    '        .FormFields("fldZip").Result = Me!Zip
    '        .FormFields("fldSIC").Result = Me!SIC
    '        .FormFields("fldNatureOfBusiness").Result = Me!NatureOfBusiness
    '        .FormFields("fldContribMed").Result = Me!ContribMed
    '        .FormFields("fldContribDen").Result = Me!ContribDen
    '        .FormFields("fldContribVision").Result = Me!ContribVision
    '        .FormFields("fldContribBLife").Result = Me!ContribBLife
    '        .FormFields("fldContribSTD").Result = Me!ContribSTD
    '        .FormFields("fldContribLTD").Result = Me!ContribLTD
    '        .FormFields("fldRenewal").Result = Me!Renewal
    '        .FormFields("fldEligibility").Result = Me!Eligibility
    '        .FormFields("fldQuoteBy").Result = Me!QuoteBy
    '        .Activate
    '        .SaveAs ("C:\Documents and Settings\jloudermilk\Desktop\DB Test Docs\RFPs\RFP " & Me!CustName & " " & Year(Date) & ".doc")
        End With
        
    Exit_OpenWord:
        Set wordApp = Nothing
        Set wordDoc = Nothing
        Exit Sub
    
    Err_OpenWord:
        MsgBox err.Description
        Resume Exit_OpenWord
        
    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •