none
Word Automation: sending text to headers and footers RRS feed

  • Question

  • I am using Access to send text to Word via bookmarks. I have bookmarks in headers and footers, but when I try to send the text to into them I get the error: Word cannot find the requested bookmark.

    I am using the following code to send text to Access

    ' Create a Word document from template.
    
    Dim WordApp As Word.Application
    
    Dim strTemplateLocation As String
    
    Dim myVariable as String 
    myVariable = “TEST!!”
    
    
    
    ' Specify location of template
    
    strTemplateLocation = Left(CurrentDb.Name, InStrRev(CurrentDb.Name, "\")) & "test.dot"
    
    
    
    Set WordApp = GetObject(, "Word.Application")
    
    If Err.Number <> 0 Then
    
    Set WordApp = CreateObject("Word.Application")
    
    End If
    
    
    
    WordApp.Visible = True
    
    WordApp.WindowState = wdWindowStateMaximize
    
    WordApp.Documents.Add Template:=strTemplateLocation, NewTemplate:=False
    
    
    
    
    
    ' Replace each bookmark with field contents.
    
    WordApp.Selection.GoTo what:=wdGoToBookmark, Name:="myBookmark"
    
    WordApp.Selection.TypeText myVariable
    
    
    
    DoEvents
    
    WordApp.Activate
    
    Set WordApp = Nothing
    
    

    I am using Office 2003 file formats.

     

    I've tried testing with the following code in the template's module and it does seem to find the bookmark:

    Sub GotoHeaderBookmark()
    
     If ActiveDocument.Bookmarks.Exists("myBookmark") = True Then
    
      ActiveDocument.Bookmarks("myBookmark").Select
    
     End If
    
    End Sub
    
    




    • Moved by Bruce Song Thursday, June 30, 2011 6:02 AM word related problem (From:Access for Developers)
    • Edited by Rick131 Sunday, July 3, 2011 12:19 AM
    Wednesday, June 29, 2011 5:58 PM

Answers

  • I ran this code in a module in the word template and it DID find the bookmark on the header but NOT the one on the footer.


    Hi Rick

    Well, actually, since bookmark names must be unique in a document it shouldn't be necessary to perform any loops at all. The following should work:

    doc.Bookmarks("Bookmarkname").Range.Text = "abc"

    If you're concerned whether the bookmark may or may not be present, then first check whether it exists:

    If doc.Bookmarks.Exists("bookmarkname") Then_
       doc.Bookmarks("Bookmarkname").Range.Text = "abc"


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Rick131 Wednesday, July 6, 2011 4:13 PM
    Friday, July 1, 2011 8:36 AM
    Moderator

All replies

  • I'll see if I can dig out the answer to this later - as far as I remember, you've got to think laterally here since as far as I remember, the "Bookmarks" object within the Headers and Footers is separate from the Bookmarks object for the main document. (Think, for example, how your main document comprises pages, and is paginated, in a slightly different way to how your headers behave.) You might have more luck in an MS Word forum if I don't get to this in time...
    Matthew Slyman M.A. (Camb.)
    Wednesday, June 29, 2011 6:10 PM
  • Maybe this helps, or not, but I already have a bookmark with the same sent data. After the data is sent the bookmark is deleted, but if I could conver to a field or something.
    Wednesday, June 29, 2011 6:20 PM
  • Have you made sure each bookmark has a unique name? I vaguely recall that as being important for Bookmarks.

    I'm going to ask the moderators to move this discussion to the "Word for Developers" forum. As you can see, I'm a bit rusty on Word VBA.


    Matthew Slyman M.A. (Camb.)
    Wednesday, June 29, 2011 9:21 PM
  • Really appreciate the help!
    Thursday, June 30, 2011 11:47 AM
  • Try this:

    Sub HdFtBookmarks()
      Dim doc As Word.Document
      Dim scn As Word.Section
      Dim bmk As Word.Bookmark
      Dim rng As Word.Range
      Dim hdft As Word.HeaderFooter
      
      Set doc = Word.ActiveDocument
      
      With doc
       For Each scn In .Sections
        For Each hdft In scn.Headers
          For Each bmk In hdft.Range.Bookmarks
            If bmk.Name = "myBookmark" Then
              Set rng = bmk.Range
              MsgBox "Found Bookmark"
            End If
          Next bmk
        Next hdft
       Next scn
      End With
    
    End Sub
    

     


    Kind Regards, Rich ... http://greatcirclelearning.com
    • Proposed as answer by Matthew Slyman Thursday, June 30, 2011 12:19 PM
    Thursday, June 30, 2011 12:00 PM
  • This looks correct to me, based on my experience - I'm going to propose this as the answer.

    Rick?


    Matthew Slyman M.A. (Camb.)
    Thursday, June 30, 2011 12:19 PM
  • There are nice articles on how to accomplish this, and to understand and learn the process as well:

    Word Automation, Part I: Accessing Word From Inside Access

    http://www.databaseadvisors.com/newsletters/newsletter072002/0207wordautomationlpt1.asp

    Word Automation, Part II: Inserting Text into Word Bookmarks

    http://www.databaseadvisors.com/newsletters/newsletter072002/0207wordautomationpt2.asp

     

    How to automate Word with Visual Basic to create a Mail Merge, for example:

    http://support.microsoft.com/kb/285332

     

    I hope this give you some heads up and may it be usefull to you.

     


    Daniel van den Berg | Washington, USA | "Anticipate the difficult by managing the easy"
    Thursday, June 30, 2011 2:20 PM
  • I ran this code in a module in the word template and it DID find the bookmark on the header but NOT the one on the footer.
    Thursday, June 30, 2011 8:12 PM
  • Look at this loop:

     For Each hdft In scn.Headers
       For Each bmk In hdft.Range.Bookmarks
        If bmk.Name = "myBookmark" Then
         Set rng = bmk.Range
         MsgBox "Found Bookmark"
        End If
       Next bmk
      Next hdft
    
    

    Have you tried seeing if there's a "scn.Footers" Collection too? Another loop might do the trick...


    Matthew Slyman M.A. (Camb.)
    Thursday, June 30, 2011 8:59 PM
  • You have to loop thru each separately. The code would look like this.

      With doc
       For Each scn In .Sections
        For Each hdft In scn.Headers
          For Each bmk In hdft.Range.Bookmarks
            If bmk.Name = "myBookmark" Then
              Set rng = bmk.Range
              MsgBox "Found Bookmark"
            End If
          Next bmk
        Next hdft
        For Each hdft In scn.Footers
          For Each bmk In hdft.Range.Bookmarks
            If bmk.Name = "myBookmark" Then
              Set rng = bmk.Range
              MsgBox "Found Bookmark"
            End If
          Next bmk
        Next hdft
       Next scn
      End With
    
    

     


    Kind Regards, Rich ... http://greatcirclelearning.com
    Thursday, June 30, 2011 11:06 PM
  • I ran this code in a module in the word template and it DID find the bookmark on the header but NOT the one on the footer.


    Hi Rick

    Well, actually, since bookmark names must be unique in a document it shouldn't be necessary to perform any loops at all. The following should work:

    doc.Bookmarks("Bookmarkname").Range.Text = "abc"

    If you're concerned whether the bookmark may or may not be present, then first check whether it exists:

    If doc.Bookmarks.Exists("bookmarkname") Then_
       doc.Bookmarks("Bookmarkname").Range.Text = "abc"


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Rick131 Wednesday, July 6, 2011 4:13 PM
    Friday, July 1, 2011 8:36 AM
    Moderator
  • Cindy,

    Have you tested your code?

    I vaguely recall that this technique doesn't work... That doc.Bookmarks() only contains the Bookmarks within the main body of the document.

    Am I wrong? Anyone?

    Matthew


    Matthew Slyman M.A. (Camb.)
    Friday, July 1, 2011 9:02 AM
  • Cindy,

    Have you tested your code?

    I vaguely recall that this technique doesn't work... That doc.Bookmarks() only contains the Bookmarks within the main body of the document.

    Am I wrong? Anyone?


    You're very wrong :-)

    There are two ways to address bookmarks, through the Document object and through the Range object. If you address them through the Range object (example: doc.Content.Bookmarks) then you'll only pick up the bookmarks in that range. Very logical. But if you address the entire document, Word processes the entire document.

    Try it yourself :-)


    Cindy Meister, VSTO/Word MVP
    Friday, July 1, 2011 9:07 AM
    Moderator
  • The simpler the better! :-)
    Kind Regards, Rich ... http://greatcirclelearning.com
    Friday, July 1, 2011 1:01 PM
  • Tried it using me original code and get the syntax error: "The requested member of the collection does not exist."

    WordApp.Selection.Bookmarks(myBookmark).Range.Text = myVariable 
    

    Using the following I get the error: "Method or data member not found"

    WordApp.ActiveDocumentBookmarks(myBookmark).Range.Text = myVariable 
    
    

     

    Sunday, July 3, 2011 12:15 AM
  • Tried it using me original code and get the syntax error: "The requested member of the collection does not exist."

    WordApp.Selection.Bookmarks(myBookmark).Range.Text = myVariable 
    
    

    Using the following I get the error: "Method or data member not found"

    WordApp.ActiveDocumentBookmarks(myBookmark).Range.Text = myVariable 
    
    
    
    

     


    In the case of the first, it's probably because the bookmark isn't in the selection.

    In the case of the second, assuming you copied and pasted the code snippet you show, you need a "." between ActiveDocument and Bookmarks. There's no such thing as ActiveDocumentBookmarks.


    Cindy Meister, VSTO/Word MVP
    • Proposed as answer by Bruce Song Wednesday, July 6, 2011 12:17 PM
    Monday, July 4, 2011 8:43 AM
    Moderator
  • Thought it was a typo, but yeah that was it.

    Thanks!!

    Wednesday, July 6, 2011 4:13 PM