Filling in Word document with Access values

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mlevit
    New Member
    • Jun 2007
    • 30

    Filling in Word document with Access values

    Hi,

    I've found a tutorial that shows you how to fill in Word documents with values from Access

    Code:
    '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:\BLANK INSTRUCTION_TEMPLATE.docx", , True)
    With doc
    .FormFields("fldCustomerID").Result = Me!CustomerID
    .FormFields("fldCompanyName").Result = Me!CompanyName
    .FormFields("fldContactName").Result = Me!ContactName
    .FormFields("fldContactTitle").Result = Me!ContactTitle
    .FormFields("fldAddress").Result = Me!Address
    .FormFields("fldCity").Result = Me!City
    .FormFields("fldRegion").Result = Me!Region
    .FormFields("fldPostalCode").Result = Me!PostalCode
    .FormFields("fldCountry").Result = Me!Country
    .FormFields("fldPhone").Result = Me!Phone
    .FormFields("fldFax").Result = Me!Fax
    .Visible = True
    .Activate
    End With
    Set doc = Nothing
    Set appWord = Nothing
    Exit Sub
    errHandler:
    MsgBox Err.Number & ": " & Err.Description
    I am using Office 2007 and it doesn't seem to be working. I can see that WINWORD.exe is open in my Task Manager but the actual word document is not launching.

    Does anybody know how to modify the above code to make it work with Office 2007.

    Thanks,

    Marat
  • puppydogbuddy
    Recognized Expert Top Contributor
    • May 2007
    • 1923

    #2
    In order for the code to work as shown, you should have placed it in a button click event on the Access Customer Form. Did you do that?

    Also, verify that the file path to your word doc is correct in your code.

    If the above is not the answer, place code breaks in the code and find out if your code executes to the breakpoint.

    Comment

    • mlevit
      New Member
      • Jun 2007
      • 30

      #3
      Originally posted by puppydogbuddy
      In order for the code to work as shown, you should have placed it in a button click event on the Access Customer Form. Did you do that?

      Also, verify that the file path to your word doc is correct in your code.

      If the above is not the answer, place code breaks in the code and find out if your code executes to the breakpoint.
      The code is under a button and everything is fine. The file is in the right place and all the code gets executed, but this code was written to use Office 2003 and it doesn't seem to work completely with Office 2007.

      Like I said above, when the code executes, the WINWORD.exe process runs in Task Manager, but the actual document does not appear in the foreground.

      I am just wondering if anyone knows how to fix the problem to allow the code to work with Office 2007.

      Thanks

      Comment

      • puppydogbuddy
        Recognized Expert Top Contributor
        • May 2007
        • 1923

        #4
        try changing line 26 to:
        appWord.Visible = True
        Last edited by puppydogbuddy; Jul 12 '08, 05:58 AM. Reason: typo

        Comment

        • mlevit
          New Member
          • Jun 2007
          • 30

          #5
          Originally posted by puppydogbuddy
          try changing line 26 to:
          appWord.Visible = True
          Thanks man, that worked. But now it's not actually filling in the values from access.

          Any ideas?

          Comment

          • puppydogbuddy
            Recognized Expert Top Contributor
            • May 2007
            • 1923

            #6
            Originally posted by mlevit
            Thanks man, that worked. But now it's not actually filling in the values from access.

            Any ideas?
            Not sure what you mean......was it filling the document before you made the above change? or has it never worked??

            try moving lines 26 and 27 (below) to between line 13 and line 14.

            appWord.visible = True
            doc.activate
            Last edited by puppydogbuddy; Jul 13 '08, 12:49 AM. Reason: typo

            Comment

            • mlevit
              New Member
              • Jun 2007
              • 30

              #7
              Originally posted by puppydogbuddy
              Not sure what you mean......was it filling the document before you made the above change? or has it never worked??

              try moving lines 26 and 27 (below) to between line 13 and line 14.

              appWord.visible = True
              doc.activate
              Well the document never appeared on the screen (but now does after i inserted appWord.visible = True), so I'm not sure if it ever filled the data from Access (but I don't think it did).

              The Word document opens but the fields that I want filled in with data from Access do not get filled in. I'll try your suggestion when I get home from work.

              Thanks

              Comment

              • mlevit
                New Member
                • Jun 2007
                • 30

                #8
                Ok I've tried moving the lines as you recommended but nothing. The Word document opens but none of the fields that I have in the Word document get filled in.

                Maybe you could try doing the tutorial yourself quickly and see if it works for you?

                Comment

                • puppydogbuddy
                  Recognized Expert Top Contributor
                  • May 2007
                  • 1923

                  #9
                  Originally posted by mlevit
                  Ok I've tried moving the lines as you recommended but nothing. The Word document opens but none of the fields that I have in the Word document get filled in.

                  Maybe you could try doing the tutorial yourself quickly and see if it works for you?
                  If you read the followup posts to the tutorial, there were several people that could not get it working. Here is the response from Susan Harkins, creator of the tutorial

                  1. Be sure to reference the Microsoft Office Object Library. Using the VB Editor, choose References from the Tools menu, check, the library, close the dialog, and return to the document and try again

                  2. I'm sorry for the confusion. Use the "bookmark" property to name the form fields. I think if you make that change, it'll work fine for you. Let me know.

                  see this link:

                  Comment

                  • mlevit
                    New Member
                    • Jun 2007
                    • 30

                    #10
                    The bookmark property is the name of the Form Field in Office 2003, but I can't see that same property in Office 2007.

                    When I add the Form Fields to my Word document, I can set their Title, but I'm not sure if that is their name that I would use in .FormFields("du eDateTem").Resu lt = DateDueToClient .Value.

                    I know there is a comment on the tutorial: "Hi is the office 2007 version avaliable yet? If so where can i find it please? "

                    So I don't think I'm the only one having the problem with Office 2007.

                    Comment

                    • puppydogbuddy
                      Recognized Expert Top Contributor
                      • May 2007
                      • 1923

                      #11
                      Originally posted by mlevit
                      The bookmark property is the name of the Form Field in Office 2003, but I can't see that same property in Office 2007.

                      When I add the Form Fields to my Word document, I can set their Title, but I'm not sure if that is their name that I would use in .FormFields("du eDateTem").Resu lt = DateDueToClient .Value.

                      I know there is a comment on the tutorial: "Hi is the office 2007 version avaliable yet? If so where can i find it please? "

                      So I don't think I'm the only one having the problem with Office 2007.
                      I don't have word 2007, but the problem seems to be lack of documentation for word 2007 at this period in time. I looked, but could not find any solid info. Let's see if we can get one form field working.

                      Try replacing line 15 with lines 15a and 15b, then tell me what happens.

                      With doc
                      15. .FormFields("fl dCustomerID").R esult = Me!CustomerID


                      With doc
                      15a. .Bookmarks.Add Range:=Selectio n.Range, Name:= "fldCustome rID"

                      15b. Me!CustomerID = bookmarks("fldC ustomerID").ran ge

                      Comment

                      Working...