Please Note: This article is written for users of the following Microsoft Word versions: 97, 2000, 2002, and 2003. If you are using a later version (Word 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Word, click here: Setting a VBA Variable from a Bookmark.

Setting a VBA Variable From a Bookmark

Written by Allen Wyatt (last updated October 25, 2018)
This tip applies to Word 97, 2000, 2002, and 2003


8

As part of a macro, you may have a need to work with information stored in a bookmark. For instance, you may need to extract the text in a bookmark, assign it to a variable, and then do some processing based on the variable contents.

There are two ways you can assign the contents of a bookmark to a variable in a VBA macro. The first is to simply jump to the bookmark and select it, then make the variable equal to the contents of the selection. The following code lines will perform this action for a bookmark named MyBookmark:

Dim sMyString As String
Selection.GoTo What:=wdGoToBookmark, Name:="MyBookmark"
sMyString = Selection.Text

If you don't want to change the selection within the document, you can also simply work with the Bookmarks collection maintained by Word. Assuming you still need the contents of the MyBookmark bookmark, the following code will do the trick:

Dim sMyString As String
sMyString = ActiveDocument.Bookmarks("MyBookmark").Range.Text

Note:

If you would like to know how to use the macros described on this page (or on any other page on the WordTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (1595) applies to Microsoft Word 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Word (Word 2007 and later) here: Setting a VBA Variable from a Bookmark.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Evaluating Formulas

Need a bit of help in figuring out how Excel is evaluating a particular formula? It's easy to figure out if you use the ...

Discover More

Extracting File Names from a Path

If you have a full path designation for the location of a file on your hard drive, you may want a way for Excel to pull ...

Discover More

Exact Matches with DSUM

The DSUM function is very handy when you need to calculate a sum based on data that matches criteria you specify. If you ...

Discover More

Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!

More WordTips (menu)

Defining a Shortcut for a Macro

You can make running macros very easy if you assign a shortcut key to the macro. This tip demonstrates how easy it is to ...

Discover More

Moving the Insertion Point in a Macro

One of the common things done in macros is to somehow "process" documents, which often means moving the insertion point ...

Discover More

Adding a Macro to a Toolbar

One of the easiest ways to quickly access a macro is to assign it to a toolbar button. How you make the assignment ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 8 + 7?

2020-10-07 06:36:30

Stanley Sendek

Dear Mr Wyatt

I would like to ask how to associate the command button to fill-in fields (bookmarks) in a word file. Let us say I have a docx file with fill-in fields (updated and filled in through popup windows through CTRL+F9 like in this video https://www.youtube.com/watch?v=4ZBUN9oH7Tg) and I would like to fill them all at once through a click on a command button. I inserted a simple command button and put in the VBA code:

Private Sub CommandButton1_Click()
ActiveDocument.FormFields(xxxxx).Result = InputBox(xxx)
End Sub

But it does not work because I have not associated the button with the fill-in bookmarks..Thanks for your suggestions.
Stanley


2017-10-21 23:15:08

Ango

is it possible to insert text from a bookmark in a closed word file into a new word doc via excel vba - the bookmark names are in an excel list - can anyone help?


2015-05-01 12:21:49

Hannah

Currently I have language that generates a second page if needed based on a selection but I need to add a bookmark to the second page. How do I add a bookmark to the second page when it doesn't show up unless it needs to show up.


2015-04-29 06:14:43

Jaume Estrany Mas

How do I capture the value of a form field drop-down list in Word?
I used "Msgbox (ActiveDocument.Bookmarks (" list "). Range.Text" and not working.
If it is a text form field if it works.
Greetings, Jaume


2015-04-02 11:39:31

DonnaS

Hello, I am working on a project in Word 2013. Here's my last item. I have a drop-down list that allows for a search of a city. Once chosen, a second drop-down list is populated. The user picks from this list and now has two items chosen. For instance: City, Venue. I have this part complete and working.

Once we have a city and a venue I want the bottom part of the Word doc to fill with the details of that Venue. I have those stored and could make each a bookmark tied to the Venue? Or?

I'm stumped how to do this. I'm thinking bookmarks, but the second drop-down is a bookmark entitled ddVenue and because the value is always changing it doesn't seem possible. I'm open to QuickParts too. Or? It needs to happen automatically without user involvement.

I'm thinking it will probably need to be some type of VBA code that makes the bookmark variable, but I'm not certain how to do this. VBA is my weakness. Any thoughts? Direction?

Thank you!
Donna


2015-03-16 07:07:59

Ken Endacott

If you also want the text within each bookmark:

Sub TypeBookmarks()
Dim BookmarkName() As String
Dim aBookmark As Bookmark
Dim k As Long
Dim ignoreCode As String
ignoreCode = "" ' change to "-" to hide hidden bookmarks
ReDim BookmarkName(0)
k = 1
For Each aBookmark In ActiveDocument.Bookmarks
If Left(aBookmark.Name, 1) <> ignoreCode Then
ReDim Preserve BookmarkName(k)
BookmarkName(k) = "Name:" & aBookmark.Name & " Text:" & aBookmark.Range.Text
k = k + 1
End If
Next aBookmark

Selection.EndKey Unit:=wdStory
For k = 0 To UBound(BookmarkName)
Selection.TypeText vbCrLf & BookmarkName(k)
Next k
End Sub


2015-03-16 06:58:11

Ken Endacott

Sub TypeBookmarks()
Dim BookmarkName() As String
Dim aBookmark As Bookmark
Dim k As Long
Dim ignoreCode As String
ignoreCode = "" ' change to "-" to hide hidden bookmarks
ReDim BookmarkName(0)
k = 1
For Each aBookmark In ActiveDocument.Bookmarks
If Left(aBookmark.Name, 1) <> ignoreCode Then
ReDim Preserve BookmarkName(k)
BookmarkName(k) = aBookmark.Name
k = k + 1
End If
Next aBookmark

Selection.EndKey Unit:=wdStory
For k = 0 To UBound(BookmarkName)
Selection.TypeText vbCrLf & BookmarkName(k)
Next k
End Sub


2015-03-16 02:03:59

Johannes van der Zwan

Good day
I would like to see a macro that creates a sorted array of all the bookmarks and insert the array at the end of my document.

In the hope you can help me with this.
Thank you very much.
Johannes


This Site

Got a version of Word that uses the menu interface (Word 97, Word 2000, Word 2002, or Word 2003)? This site is for you! If you use a later version of Word, visit our WordTips site focusing on the ribbon interface.

Videos
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.