
06-28-2016, 12:30 PM
|
Novice
|
|
Join Date: Aug 2010
Posts: 6
|
|
How to add a bookmark to text within a header
How to add a bookmark to text within a header
I am a novice to VBA, and have gathered some VBA from the web to suit my needs. However I am trying to add a bookmark to text within a heading.
The heading is of the format:
1.1.1.1.1 Register Name Verbose | register_name | register address.
I have used the following code to find all the headings of level 5, extracted the register name from the heading and put it into a variable to be used for the bookmark name, however I am unable to add the bookmark.
I am getting an error when I try to add the bookmark saying it requires an object. I thought I was providing the object with the bookmark part, but not sure what to do.
Thanks for any help,
Jim
----------------------------------------------------------------------------
Sub Traverse_Headings()
'Define the variables
Dim heading As Range
Dim myHeadings As Variant
Dim first_bar
Dim sec_bar
Set heading = ActiveDocument.Range(Start:=0, End:=0)
myHeadings = ActiveDocument.GetCrossReferenceItems(wdRefTypeHea ding)
For icount = LBound(myHeadings) To UBound(myHeadings)
' Subtract
Level = GetLevel(CStr(myHeadings(icount)))
If Level = 5 Then
' Find the position of the bars in the heading. The text in the middle is
' what we are looking for. So find the first bar, find the second bar, and then use the mid function to find the middle
first_bar = InStr(1, myHeadings(icount), "|", 1)
sec_bar = InStrRev(myHeadings(icount), "|")
reg_name = Mid(myHeadings(icount), first_bar + 2, sec_bar - first_bar - 2)
bkmark_name = "C3_" & reg_name & "_desc"
'Once we find that there is a register name need to create the bookmark based on the positions found above.
With ActiveDocument.Bookmarks
.Add Range:=myHeadings(icount).Range, Name:=bkmark_name
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
End If
Next icount
End Sub
|