Concerned_Citizen
Concerned_Citizen

Reputation: 6845

Inserting into Multiple Bookmark Locations - Excel VBA

I am using an Excel macro where I am inserting the same text to several bookmarks in a Word doc. How can I do this by specifying the insert command once and apply it to all the bookmark locations?

Now I am doing the following for all bookmarks?

Dim monYear As String
monYear = Format(DateAdd("m", -1, Now), "mmmm yyyy")


wdApp.Selection.GoTo what:=-1, Name:="Front_Page_Month_Year"
wdApp.Selection.TypeText monYear

wdApp.Selection.GoTo what:=-1, Name:="Page2_Month_Year"
wdApp.Selection.TypeText monYear

And on and on....

Upvotes: 0

Views: 1122

Answers (1)

Cindy Meister
Cindy Meister

Reputation: 25693

There's no way to use a single command to write to multiple bookmark locations. What you can do, however, is reference one bookmark location in order to display that bookmark's content in multiple locations.

Create the bookmark, then insert a cross-reference to it in each location where you want to display the bookmark contents. (Or create one cross-reference, then copy/paste to the other locations.)

A tip for your code: the approach you're using with the Selection object is not optimal. As in Excel, it's better to work with the object model, rather than rely on a Selection. Thus:

Dim doc as Word.Document
Set doc = wdApp.ActiveDocument 'Note: if you're opening a document, set in the Open method
doc.Bookmarks("Front_Page_Month_Year").Range.Text = monYear
'When you're done, update the REF fields (references)
doc.Fields.Update

Upvotes: 2

Related Questions