![]() |
#1
|
|||
|
|||
![]()
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 |
#2
|
|||
|
|||
![]()
myHeadings(icount) just returns the element from the array, and has no connection to the paragraph in the document.
The following code will however do what you want: Code:
Dim bkmarkname As String Selection.HomeKey wdStory Selection.Find.ClearFormatting Selection.Find.Style = ActiveDocument.Styles("Heading 5") With Selection.Find Do While .Execute(FindText:="", MatchWildcards:=False, Forward:=True, Wrap:=wdFindStop) = True With Selection bkmarkname = Mid(.Range.Text, InStr(.Range.Text, "|") + 2, InStrRev(.Range.Text, "|") - InStr(.Range.Text, "|") - 2) ActiveDocument.Bookmarks.Add Trim(bkmarkname), .Range .Collapse wdCollapseEnd End With Loop End With |
#3
|
|||
|
|||
![]()
Doug - when I use this it performs this against all level 5 headings. Is there a way of only doing this for heading 5's that contain the | would I just be able to loop through the heading and if the | is not found then move on?
|
#4
|
|||
|
|||
![]()
Use:
Code:
Dim bkmarkname As String Selection.HomeKey wdStory Selection.Find.ClearFormatting Selection.Find.Style = ActiveDocument.Styles("Heading 5") With Selection.Find Do While .Execute(FindText:="", MatchWildcards:=False, Forward:=True, Wrap:=wdFindStop) = True With Selection If Instr(.Range.Text, "|") > 0 then bkmarkname = Mid(.Range.Text, InStr(.Range.Text, "|") + 2, InStrRev(.Range.Text, "|") - InStr(.Range.Text, "|") - 2) ActiveDocument.Bookmarks.Add Trim(bkmarkname), .Range End If .Collapse wdCollapseEnd End With Loop End With |
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Find Bookmark, move to bookmark, execute code, repeat | raymm3852 | Word VBA | 10 | 04-15-2016 06:21 PM |
Add a bookmark to the Header using Word VBA | ChrisDevrell | Word VBA | 2 | 02-06-2015 04:15 PM |
Adding field in word in header in a bookmark in table with Excel vba Late Binding | Hdr | Excel Programming | 6 | 02-11-2013 02:58 AM |
![]() |
Amapola188 | Word VBA | 3 | 07-12-2012 05:16 PM |
![]() |
skarden | Word | 1 | 12-12-2011 10:39 PM |