Loading
Word.Tips.Net WordTips (Menu Interface)

Setting a VBA Variable From a Bookmark

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.

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

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.

Related Tips:

Learning Made Easy! Quickly teach yourself how to format, publish, and share your content using Word 2013. With Step by Step, you set the pace, building and practicing the skills you need, just when you need them! Check out Microsoft Word 2013 Step by Step today!

 

Leave your own comment:

*Name:
Email:
  Notify me about new comments ONLY FOR THIS TIP
Notify me about new comments ANYWHERE ON THIS SITE
Hide my email address
*Text:
*What is 5+3 (To prevent automated submissions and spam.)
 
 
           Commenting Terms

Comments for this tip:

Hannah    01 May 2015, 12:21
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.
Jaume Estrany Mas    29 Apr 2015, 06:14
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
DonnaS    02 Apr 2015, 11:39
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
Ken Endacott    16 Mar 2015, 07:07
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
Ken Endacott    16 Mar 2015, 06:58
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
Johannes van der Zwan    16 Mar 2015, 02:03
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
 
 

Our Company

Sharon Parq Associates, Inc.

About Tips.Net

Contact Us

 

Advertise with Us

Our Privacy Policy

Our Sites

Tips.Net

Beauty and Style

Cars

Cleaning

Cooking

DriveTips (Google Drive)

ExcelTips (Excel 97–2003)

ExcelTips (Excel 2007–2016)

Gardening

Health

Home Improvement

Money and Finances

Organizing

Pests and Bugs

Pets and Animals

WindowsTips (Microsoft Windows)

WordTips (Word 97–2003)

WordTips (Word 2007–2016)

Our Products

Helpful E-books

Newsletter Archives

 

Excel Products

Word Products

Our Authors

Author Index

Write for Tips.Net

Copyright © 2016 Sharon Parq Associates, Inc.