none
Locating a bookmark in a word footer RRS feed

  • Question

  • I am having problems finding a bookmark in a Word doucument footer and entering text.

    I recorded the following macro which worked when I recorded it but failed to run returning the message 'Word cannot find the requested bookmark'

    Sub test()
    '
    ' test Macro
    '
    '
        WordBasic.ViewFooterOnly
        Selection.GoTo What:=wdGoToBookmark, Name:="test"
        With ActiveDocument.Bookmarks
            .DefaultSorting = wdSortByName
            .ShowHidden = True
        End With
        Selection.TypeText Text:="Mike Potts"
        ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
    End Sub

    I am using Word 2007.  Ultimately I wish to do this from a VB application (VB 6.0 Enterprise)

    Any help would be appreciated.

     

    Friday, October 7, 2011 6:48 PM

Answers

  • It’s likely that the bookmark got deleted when you recorded the macro. It’s a known issue with VBA and bookmarks. See http://word.mvps.org/faqs/macrosvba/InsertingTextAtBookmark.htm for a workaround.

    Stefan Blom, Microsoft Word MVP
    • Marked as answer by Liliane Teng Tuesday, October 18, 2011 8:08 AM
    Friday, October 7, 2011 7:12 PM
  • Hi Mike

    An observation about your recorded code:

    You should not use Selection and SeekView to work with content in the header/footer. In complex documents it's not reliable. And in any case, it makes the screen flicker.

    Better is to work with the Range object, something that in this particular case the macro recorder can't give you. Example:

    Dim rng as Word.Range
    Set rng = wdApp.ActiveDocument.Sections(1).Headers( _
      Word.WdHeaderFooterIndex.wdHeaderPrimary).Range
    rng.Bookmarks("test").Text = "Mike Potts"

    If you don't want to lose the bookmark, then:

    Dim rng as Word.Range
    Dim bkmName as String
    Dim doc as Word.Document
    Set doc = wdApp.ActiveDocument
    bkmName = "test"
    If doc.Bookmarks.Exists(bkmName) Then
      Set rng = doc.Boomarks(bkmName).Range
      rng.Text = "Mike Potts"
      doc.Bookmarks.Add(bkmName, rng)
    End If

    You'll note that rng is used differently in the two examples. The second can also be used in the first, but I wanted to demonstrate how to access the Range of a header (or footer) in the first example. The second example works for the entire document.


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Liliane Teng Tuesday, October 18, 2011 8:08 AM
    Saturday, October 8, 2011 6:27 AM
    Moderator

All replies

  • It’s likely that the bookmark got deleted when you recorded the macro. It’s a known issue with VBA and bookmarks. See http://word.mvps.org/faqs/macrosvba/InsertingTextAtBookmark.htm for a workaround.

    Stefan Blom, Microsoft Word MVP
    • Marked as answer by Liliane Teng Tuesday, October 18, 2011 8:08 AM
    Friday, October 7, 2011 7:12 PM
  • Hi Mike

    An observation about your recorded code:

    You should not use Selection and SeekView to work with content in the header/footer. In complex documents it's not reliable. And in any case, it makes the screen flicker.

    Better is to work with the Range object, something that in this particular case the macro recorder can't give you. Example:

    Dim rng as Word.Range
    Set rng = wdApp.ActiveDocument.Sections(1).Headers( _
      Word.WdHeaderFooterIndex.wdHeaderPrimary).Range
    rng.Bookmarks("test").Text = "Mike Potts"

    If you don't want to lose the bookmark, then:

    Dim rng as Word.Range
    Dim bkmName as String
    Dim doc as Word.Document
    Set doc = wdApp.ActiveDocument
    bkmName = "test"
    If doc.Bookmarks.Exists(bkmName) Then
      Set rng = doc.Boomarks(bkmName).Range
      rng.Text = "Mike Potts"
      doc.Bookmarks.Add(bkmName, rng)
    End If

    You'll note that rng is used differently in the two examples. The second can also be used in the first, but I wanted to demonstrate how to access the Range of a header (or footer) in the first example. The second example works for the entire document.


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Liliane Teng Tuesday, October 18, 2011 8:08 AM
    Saturday, October 8, 2011 6:27 AM
    Moderator