none
Data from access to word RRS feed

  • Question

  • Hi all,

    I will output data to a word document.

    I work with office 2013

    I have made a word document and inserted different bookmarks. I have the following code. The document opens, but the data is no filled out. The reference is made with Word 15.0 object library

    The result of the query is OK. Is there something wrong?

    Here is the code:

    Dim appWord As Word.Application
    Dim doc As Word.Document
    Dim rst As DAO.Recordset
    Dim strSQL As String
    '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:\Users\luc_000\Documents\Motoren.docx")
    strSQL =SELECT different fields
    Set rst = CurrentDb.OpenRecordset(strSQL)
    With doc
      .FormFields("TAG").Result = rst!Tag
      .FormFields("PID").Result = rst!PID
      .FormFields("Omschrijving").Result = rst!Omschrijving

    'i've tried also this

      .ActiveDocument.Bookmarks("Testknop").Select
      .Selection.TypeText rst!Testknop

      .Visible = True
      .Activate
    End With
    Set doc = Nothing
    Set appWord = Nothing
    Exit Sub

    errHandler:

    MsgBox Err.Number & ": " & Err.Description

    Wednesday, October 12, 2016 8:48 PM

Answers

  • Hi,

    I suggest you use Debug.Print to check the data in this Sub and check if it is empty or not.

    Then make sure there are these formFields in your document.

    And I suggest you use the following instead of setting it to nothing.

     doc.Save

     doc.Close

     appWord.Quit

    Besides, if you want to use ActiveDocument.Bookmarks to insert the data, the formFields would be deleted after Selection.TypeText

     


    Thursday, October 13, 2016 6:48 AM
    Moderator
  • I created the following function many moons ago to update bookmarks in a Word document

    Sub UpdateBookmark(BookmarkToUpdate As String, TextToUse As String)
        Dim BMRange As Range
        Set BMRange = doc.Bookmarks(BookmarkToUpdate).Range
        BMRange.Text = TextToUse
        doc.Bookmarks.Add BookmarkToUpdate, BMRange
    End Sub

    So you'd simply call it

    Call UpdateBookmark("Testknop", Nz(rst!Testknop,""))

    Note, you'd need to make doc a public variable.

    As a side note, you might want to consider using Content Controls, they are easier to code, amongst other things.


    Daniel Pineault, 2010-2016
    Microsoft MVP Professional Support: http://www.cardaconsultants.com
    MS Access Tips and Code Samples: http://www.devhut.net


    Thursday, October 13, 2016 3:05 PM

All replies

  • Hi,

    Is this really what you have?

    strSQL = SELECT different fields

    It doesn't look right...

    Wednesday, October 12, 2016 9:13 PM
  • Hi Lteu,

    I think the SQL statement you typed is an example (you've hid your real statement for security reasons, I suppose).

    Well, I always use the Bookmark element to put data into Word document, so I suggest you the same, replacing the

      .Selection.TypeText rst!Testknop
    for
      .Selection.Text = nz(rst!Testknop,"BLANK")

    so you can also track the null fields.

    Thursday, October 13, 2016 4:38 AM
  • Hi,

    I suggest you use Debug.Print to check the data in this Sub and check if it is empty or not.

    Then make sure there are these formFields in your document.

    And I suggest you use the following instead of setting it to nothing.

     doc.Save

     doc.Close

     appWord.Quit

    Besides, if you want to use ActiveDocument.Bookmarks to insert the data, the formFields would be deleted after Selection.TypeText

     


    Thursday, October 13, 2016 6:48 AM
    Moderator
  • I created the following function many moons ago to update bookmarks in a Word document

    Sub UpdateBookmark(BookmarkToUpdate As String, TextToUse As String)
        Dim BMRange As Range
        Set BMRange = doc.Bookmarks(BookmarkToUpdate).Range
        BMRange.Text = TextToUse
        doc.Bookmarks.Add BookmarkToUpdate, BMRange
    End Sub

    So you'd simply call it

    Call UpdateBookmark("Testknop", Nz(rst!Testknop,""))

    Note, you'd need to make doc a public variable.

    As a side note, you might want to consider using Content Controls, they are easier to code, amongst other things.


    Daniel Pineault, 2010-2016
    Microsoft MVP Professional Support: http://www.cardaconsultants.com
    MS Access Tips and Code Samples: http://www.devhut.net


    Thursday, October 13, 2016 3:05 PM