Please Note: This article is written for users of the following Microsoft Word versions: 2007, 2010, 2013, 2016, 2019, Word in Microsoft 365, and 2021. If you are using an earlier version (Word 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Word, click here: Determining the Number of Bookmarks Defined in a Document.

Determining the Number of Bookmarks Defined in a Document

Written by Allen Wyatt (last updated January 13, 2024)
This tip applies to Word 2007, 2010, 2013, 2016, 2019, Word in Microsoft 365, and 2021


3

If you are writing a macro, you can determine the number of bookmarks defined in a document by using the Count property with the Bookmarks collection object. This is done using the following VBA syntax:

x = ActiveDocument.Bookmarks.Count

where x is the variable you can then use in other VBA macro lines.

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 (12096) applies to Microsoft Word 2007, 2010, 2013, 2016, 2019, Word in Microsoft 365, and 2021. You can find a version of this tip for the older menu interface of Word here: Determining the Number of Bookmarks Defined in a Document.

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

Protecting Tracked Changes

Track Changes is a great tool for editors and collaborators to use when creating documents. An author, seeking changes ...

Discover More

Using Text Boxes

Text boxes are handy for placing information in a container that can "float" over your worksheet. This tip explains what ...

Discover More

Making Backup Copies

When you save your documents, Word doesn't normally make backups of your files. If you want the program to do that, it ...

Discover More

Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!

More WordTips (ribbon)

Printing a Bookmark List with Contents

Bookmarks can be a great tool in Word, allowing you to easily remember the location of desired blocks of text. If you ...

Discover More

Reusing a Bookmark

Bookmarks in Word are just like bookmarks used in paper books, any given bookmark may be reused to mark a new location. ...

Discover More

Deleting a Bookmark

Create a bookmark and you may, at some future point, need to delete that bookmark. It's easy to do, as described in this tip.

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 6 + 7?

2024-01-16 10:08:16

Andrew

To NOT include hidden bookmarks in the count, try using "ActiveDocument.Bookmarks.ShowHidden = False" before the call to "ActiveDocument.Bookmarks.Count". See https://learn.microsoft.com/en-us/office/vba/api/word.bookmarks.showhidden.


2024-01-14 10:22:40

John Patton

Allen

Try using ChatGPT to write VBA Word macros. It’s really good starting point. I wrote

“Write VBA Word macro to delete styles not in a list”

Here is result - not perfect but good

Sub DeleteStylesNotInList()
Dim styleList As Variant
Dim style As Style

' Define the list of styles to keep
styleList = Array("Normal", "Heading 1", "Heading 2", "CustomStyle1", "CustomStyle2")

' Loop through each style in the document
For Each style In ActiveDocument.Styles
' Check if the style is not in the list
If Not IsInArray(style.NameLocal, styleList) Then
' Delete the style
style.Delete
End If
Next style
End Sub

Function IsInArray(valueToFind As Variant, arr As Variant) As Boolean
' Check if a value is in an array
Dim element As Variant
For Each element In arr
If element = valueToFind Then
IsInArray = True
Exit Function
End If
Next element
IsInArray = False
End Function


2024-01-13 05:44:16

Ken Endacott

The bookmarks count includes hidden bookmarks whose name starts with an underscore for example _Toc156072804.

Not included in the count are the 16 predefined bookmarks whose name starts with a backslash:
\Cell \Char \Doc \EndOfDoc \EndOfSel \HeadingLevel \Line \Page \Para \PrecSel2 \PrevSel1 \Section \Sel \StartOfDoc \StartOfSel \Table


This Site

Got a version of Word that uses the ribbon interface (Word 2007 or later)? This site is for you! If you use an earlier version of Word, visit our WordTips site focusing on the menu 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.