View Single Post
 
Old 07-21-2023, 08:26 AM
enigmadreama enigmadreama is offline Windows 10 Office 2016
Novice
 
Join Date: Jul 2023
Posts: 2
enigmadreama is on a distinguished road
Default Insert a bookmark for each paragraph in a multilevel list

I have a document that has a multilevel list paragraph structure, beginning with paragraph # 1.01 and ending in 9.68. There are 9 chapters/sections, and each paragraph in each section restarts at .01. For example: 1.01, 2.01, 3.01, etc. Most paragraphs are numbered X.XX, but some paragraphs have three digits after the end of the period such as 8.123. I need to create a bookmark for each paragraph and name the bookmark based on the first 4 characters of the paragraph number of the multilevel list. The code below only created bookmarks for those paragraphs ending after "." So for example, there's a bookmark for paragraph "11" after 6.11. Because "11" appears 9 times, the code doesn't run again because it thinks the paragraph number is a duplicate.

How can I tweak the code so there's a bookmark for 1.01, 2.01, 8.123, etc.?

Code:
Sub AddBookmarksWithFirstFourCharsOfParagraphNumber()
    Dim para As Paragraph
    Dim bookmarkName As String
    Dim paragraphNum As Long
   
    ' Loop through each paragraph in the active document
    For Each para In ActiveDocument.Paragraphs
        ' Check if the paragraph is a list paragraph
        If para.Range.ListFormat.ListType <> WdListType.wdListNoNumbering Then
            ' Get the paragraph number and convert it to a string
            paragraphNum = para.Range.ListFormat.ListValue
            Dim paragraphNumStr As String
            paragraphNumStr = CStr(paragraphNum)
           
            ' Generate a bookmark name with the first four characters of the paragraph number
            bookmarkName = "Bookmark_" & Left(paragraphNumStr, 4)
           
            ' Add a bookmark to the paragraph
            ActiveDocument.Bookmarks.Add bookmarkName, para.Range
        End If
    Next para
End Sub
Reply With Quote