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!
 

Some videos you may like

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.

mole999

Moderator
Joined
Oct 23, 2004
Messages
10,513
Office Version
  1. 2019
  2. 2016
  3. 2013
Platform
  1. Windows
Have you tried inserting a Table of Contents, clicking there will then allow navigation ?
 

PM_ME_YOUR_DATASETS

New Member
Joined
Sep 20, 2017
Messages
8
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.
 

PM_ME_YOUR_DATASETS

New Member
Joined
Sep 20, 2017
Messages
8
Can anyone help me here? I'm pretty sure my problem is in the first section of my code, where I cycle through the headings.
 

Watch MrExcel Video

Forum statistics

Threads
1,108,652
Messages
5,524,101
Members
409,561
Latest member
ay123

This Week's Hot Topics

Top