Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 06-28-2016, 12:30 PM
jroger05 jroger05 is offline How to add a bookmark to text within a header Windows XP How to add a bookmark to text within a header Office 2003
Novice
How to add a bookmark to text within a header
 
Join Date: Aug 2010
Posts: 6
jroger05 is on a distinguished road
Default 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
Reply With Quote
  #2  
Old 07-09-2016, 11:49 PM
DougMVP DougMVP is offline How to add a bookmark to text within a header Windows 7 32bit How to add a bookmark to text within a header Office 2010 32bit
Advanced Beginner
 
Join Date: Nov 2013
Posts: 50
DougMVP will become famous soon enough
Default

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
Reply With Quote
  #3  
Old 07-14-2016, 12:13 PM
jroger05 jroger05 is offline How to add a bookmark to text within a header Windows XP How to add a bookmark to text within a header Office 2003
Novice
How to add a bookmark to text within a header
 
Join Date: Aug 2010
Posts: 6
jroger05 is on a distinguished road
Default

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?
Reply With Quote
  #4  
Old 07-14-2016, 02:28 PM
DougMVP DougMVP is offline How to add a bookmark to text within a header Windows 7 32bit How to add a bookmark to text within a header Office 2010 32bit
Advanced Beginner
 
Join Date: Nov 2013
Posts: 50
DougMVP will become famous soon enough
Default

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
Reply With Quote
Reply



Similar Threads
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
How to add a bookmark to text within a header Open header to insert text into bookmark Amapola188 Word VBA 3 07-12-2012 05:16 PM
How to add a bookmark to text within a header Word 2003 - IncludeText Does Not Include Bookmark Text if in a Form Text Control skarden Word 1 12-12-2011 10:39 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 12:10 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft