1

I am using Excel VBA to populate bookmarks in Word. I also want to update a bookmark in the footer of the document. Whenever the bookmark is not in the footer, my code works fine. However, when the bookmark in the footer should be populated, I get a Run-time error '5678' Word cannot find the requested bookmark..

I tried recording the code in Word, which gives me the following:

Selection.Goto What:=wdGoToBookmark, Name:=Bookmarkname

Since I am working from Excel I have changed this code to, where wordobject = CreateObject("Word.Application"):

wordobject.Selection.Goto What:=wdGoToBookmark, Name:=Bookmarkname  

As stated above, this code works fine when the bookmark is in the 'normal' text. Whenever the bookmark is in the footer, it throws the error.

How can I populate bookmarks in the footer of the Word-document?

Community
  • 1
  • 1
Roosz0rd
  • 192
  • 13

1 Answers1

1

Your code would probably works, if you manually select all the footer and run it. However, you are probably not keen on such a solution. In general, using Selection in VBA always causes problems sooner or later. Thus, make a favour to your future self and consider avoiding it. How to avoid using Select in Excel VBA

This is how to change the Text of any bookmark in Word, including those on the footers and the headers (works from Word only):

Option Explicit
Sub TestMe()
    Dim bmk As Bookmark
    For Each bmk In ThisDocument.Bookmarks
        If bmk.Name = "wdGoToBookmark" Then
            bmk.Range.Text = "Something new here"
        End If
    Next bmk
End Sub

In general, to see info for the bookmarks in the immediate window, a standard loop can help (works from Word only):

Sub TestMe()
    Dim bmk As Bookmark
    For Each bmk In ThisDocument.Bookmarks
        Debug.Print bmk.Name
        Debug.Print bmk.Range.Text
    Next bmk
End Sub

If you want to make it work from Excel, then you cannot define variables as bookmarks in Excel. But something like this will do the job:

Public Sub TestMe()

    Dim wordObj     As Object
    Dim wordObjD    As Object

    Dim bmk         As Object
    Dim countBmks   As Long

    Set wordObj = CreateObject("Word.Application")
    Set wordObjD = wordObj.documents.Add("K:\yourPath\yourFile.docx")
    wordObj.Visible = True

    'Check whether a bookmark exists (FYI):
    Debug.Print wordObjD.bookmarks.exists("someBookmark")

    For Each bmk In wordObjD.Bookmarks
        Debug.Print bmk.Name
        Debug.Print bmk.Range.Text
        If bmk.Name = "TheNameOfTheBookmark" Then
            bmk.Range.Text = "SomeText"
        End If
    Next bmk

End Sub
Vityata
  • 42,633
  • 8
  • 55
  • 100
  • Thank you for your solution. I am always having troubles not using select. Could you please explain the part 'bmk.Name = "wdGoToBookmark" '? How is the name of the bookmark wdGoToBookmark and how can I look for the bookmark with the actual name (bookmarkname) I am looking for? – Roosz0rd Jan 08 '18 at 13:39
  • @Roosz0rd - the bookmark's name is set to "wdGoToBookmark" manually. The code just changes the text of the bookmark. – Vityata Jan 08 '18 at 13:41
  • I am getting a Type mismatch on 'If bmk.Name = "wdGoToBookmark" ' But when I use a msgbox(bmk.Name) it does seem to be a string.. – Roosz0rd Jan 08 '18 at 13:50
  • @Roosz0rd - that is strange. Do you have **explicitly** copied and pasted only my code or there is something else there? – Vityata Jan 08 '18 at 13:53
  • 1
    No, since I am working from Excel, ThisDocument.Bookmarks would not work. For Each bmk In wordobject.ActiveDocument.Bookmarks If bmk.Name Is "wdGoToBookmark" Then bmk.Range.Text = sBookmarkTekst End if next bmk – Roosz0rd Jan 08 '18 at 14:19
  • @Roosz0rd - I have managed to make it work from Excel as well :) – Vityata Jan 08 '18 at 14:53