Written by Allen Wyatt (last updated January 13, 2024)
This tip applies to Word 2007, 2010, 2013, 2016, 2019, 2021, and Word in Microsoft 365
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:
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, 2021, and Word in Microsoft 365. 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.
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!
Want to get to a bookmark location quickly? One option is to use the F5 key to jump to a bookmark. Perhaps a quicker ...
Discover MoreDo you want an easy way to see all the bookmarks in your document? Word provides a way to make them visible, or you can ...
Discover MoreNeed to get rid of a lot of bookmarks all at once? Word doesn't provide a way to do it, but you can use the short macro ...
Discover MoreFREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
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
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.
Visit the WordTips channel on YouTube
FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2025 Sharon Parq Associates, Inc.
Comments