Answered by:
Display bookmark name

Question
-
I have a Word 2010 document that has about 300 fields with bookmarks, I'm trying to figure out how to display the bookmark names in the document. The document is imported into a vb6 app and I need to see the bookmark names so I can add some code in vb to get the text in the field.
I checked the "Show bookmarks" under the Word Option -> Advanced -> Show documnet content, but I only get the field name inclosed in brackets ( [] ), how do I get the actual bookmark to display.
I tried with the document protected and unprotected.
Thanks for your help
Dan
Dan1104
Tuesday, January 8, 2013 1:55 PM
Answers
-
I don't think there's a built-in way.
You could run a macro to insert the names next to the bookmarks. e.g. the following macros will put subscripted names next to bookmarks where it can, and remove the text (you can make the text removal tests a bit more cautious if you want. It may not be able to insert text at all the places it needs to, hence the "On Error Resume"
' adjust these as necessary,
' especially to avoid conflicts
' with ordinary document text
' Use a typeface with useful characters
Const BNFontName As String = "Arial Unicode MS"
Const BNFontSize As Single = 6
Const BNFontPosition As Single = 6
Const BNPrefix As Long = &H2199
Const BNSuffix As Long = &H22C5
Const BNColorIndex As Integer = WdColorIndex.wdBrightGreen
Sub insertBMNames()
Dim b As Word.Bookmark
Dim r As Word.Range
On Error Resume Next
With ActiveDocument
For Each b In .Bookmarks
Set r = b.Range
With r
.Collapse direction:=wdCollapseEnd
.InsertAfter ChrW(BNPrefix) & b.Name & ChrW(BNSuffix)
With .Font
.Name = BNFontName
.Size = BNFontSize
.Position = BNFontPosition
End With
End With
Next
End With
End Sub
Sub removeBMNames()
On Error Resume Next
With ActiveDocument.Content.Find
.Font.Name = BNFontName
.Font.Size = BNFontSize
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Execute Replace:=WdReplace.wdReplaceAll
End With
End Sub
Peter Jamieson
- Marked as answer by 许阳(无锡) Friday, January 11, 2013 4:08 AM
Tuesday, January 8, 2013 3:42 PM
All replies
-
I don't think there's a built-in way.
You could run a macro to insert the names next to the bookmarks. e.g. the following macros will put subscripted names next to bookmarks where it can, and remove the text (you can make the text removal tests a bit more cautious if you want. It may not be able to insert text at all the places it needs to, hence the "On Error Resume"
' adjust these as necessary,
' especially to avoid conflicts
' with ordinary document text
' Use a typeface with useful characters
Const BNFontName As String = "Arial Unicode MS"
Const BNFontSize As Single = 6
Const BNFontPosition As Single = 6
Const BNPrefix As Long = &H2199
Const BNSuffix As Long = &H22C5
Const BNColorIndex As Integer = WdColorIndex.wdBrightGreen
Sub insertBMNames()
Dim b As Word.Bookmark
Dim r As Word.Range
On Error Resume Next
With ActiveDocument
For Each b In .Bookmarks
Set r = b.Range
With r
.Collapse direction:=wdCollapseEnd
.InsertAfter ChrW(BNPrefix) & b.Name & ChrW(BNSuffix)
With .Font
.Name = BNFontName
.Size = BNFontSize
.Position = BNFontPosition
End With
End With
Next
End With
End Sub
Sub removeBMNames()
On Error Resume Next
With ActiveDocument.Content.Find
.Font.Name = BNFontName
.Font.Size = BNFontSize
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Execute Replace:=WdReplace.wdReplaceAll
End With
End Sub
Peter Jamieson
- Marked as answer by 许阳(无锡) Friday, January 11, 2013 4:08 AM
Tuesday, January 8, 2013 3:42 PM -
Thanks, I used it today and worked like a charm.
Russ Neuman
Wednesday, March 28, 2018 7:05 PM