Converting Headings to Bookmarks in Word

PM_ME_YOUR_DATASETS

New Member
Joined
Sep 20, 2017
Messages
8
Hey all. I'm using Office 2016. I'd like to make a macro that loops through each heading in a document, and then creates a bookmark at the heading's location using the heading text (modified as necessary) as the bookmark name. Most of the headings are in X.X.X.X format, such as "3.3.4.1. sometexthere".

I'm still a beginner using VBA, but after a lot of googling I managed to adapt some Frankenstein code that almost works:

Code:
Sub HeadingsToBookmarks()
 
        Dim heading As Range
        Set heading = ActiveDocument.Range(Start:=0, End:=0)
        Do
            Dim current As Long
            current = heading.Start
            Set heading = heading.GoTo(What:=wdGoToHeading, Which:=wdGoToNext)
            If heading.Start = current Then
                Exit Do
            End If
            ActiveDocument.Bookmarks.Add MakeValidBMName(heading.Paragraphs(1).Range.Text), Range:=heading.Paragraphs(1).Range
            
        Loop
    End Sub

Function MakeValidBMName(strIn As String)
 Dim pFirstChr As String
 Dim i As Long
 Dim tempStr As String
 strIn = Trim(strIn)
 pFirstChr = Left(strIn, 1)
 If Not pFirstChr Like "[A-Za-z]" Then
 strIn = "Section_" & strIn
 End If
 For i = 1 To Len(strIn)
 Select Case Asc(Mid$(strIn, i, 1))
 Case 49 To 58, 65 To 90, 97 To 122
 tempStr = tempStr & Mid$(strIn, i, 1)
 Case Else
 tempStr = tempStr & "_"
 End Select
 Next i
 tempStr = Replace(tempStr, " ", " ")
 tempStr = Replace(tempStr, ":", "")
 
 If Right(tempStr, 1) = "_" Then
 tempStr = Left(tempStr, Len(tempStr) - 1)
 End If
 
 MakeValidBMName = tempStr
 End Function


This code almost works, and makes appropriate bookmarks at some of the headings, but not all. Can anyone help me figure out what I need to fix here, or have other recommendations on how else I can clean up this code?


Thanks!
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Have you tried inserting a Table of Contents, clicking there will then allow navigation ?
 
Upvote 0
Have you tried inserting a Table of Contents, clicking there will then allow navigation ?
Unfortunately, that won't work for me. I need to be able to link to specific sections within the document from other documents, which (as far as I'm aware) means using bookmarks. I have many lengthy documents that are all currently internally sorted using the Headings system, and I need a way to programmatically generate bookmarks from those pre-existing Headings.
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,219
Members
452,619
Latest member
Shiv1198

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top