Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 07-21-2023, 08:26 AM
enigmadreama enigmadreama is offline Insert a bookmark for each paragraph in a multilevel list Windows 10 Insert a bookmark for each paragraph in a multilevel list Office 2016
Novice
Insert a bookmark for each paragraph in a multilevel list
 
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
  #2  
Old 07-21-2023, 11:47 PM
gmayor's Avatar
gmayor gmayor is offline Insert a bookmark for each paragraph in a multilevel list Windows 10 Insert a bookmark for each paragraph in a multilevel list Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,106
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Replace
Code:
bookmarkName = "Bookmark_" & Left(paragraphNumStr, 4)
with
Code:
paragraphNumStr = Left(paragraphNumStr, InStr(1, paragraphNumStr, Chr(32)))
bookmarkName = "Bookmark_" & Trim(Replace(paragraphNumStr, ".", "_"))
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 07-26-2023, 06:39 AM
enigmadreama enigmadreama is offline Insert a bookmark for each paragraph in a multilevel list Windows 10 Insert a bookmark for each paragraph in a multilevel list Office 2016
Novice
Insert a bookmark for each paragraph in a multilevel list
 
Join Date: Jul 2023
Posts: 2
enigmadreama is on a distinguished road
Default

Thank you. The new code didn't work. For example, when I clicked on bookmark 100, it went to a paragraph on pg. 30 but this paragraph isn't part of a multilevel list. I'd like to update the code to reflect two things:
1. Each multilevel list paragraph from 1.01 through 9.68 should have a bookmark.
2. The bookmark name should have the paragraph number in it (i.e., Bookmark_968).

I don't know what is wrong with my code.
Reply With Quote
  #4  
Old 07-27-2023, 02:08 AM
gmayor's Avatar
gmayor gmayor is offline Insert a bookmark for each paragraph in a multilevel list Windows 10 Insert a bookmark for each paragraph in a multilevel list Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,106
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Your code didn't work because it returns the ListValue and not the ListString. My fault; I should have checked instead of assuming that it worked before. Try the following instead
Code:
Sub AddBookmarksWithCharsOfParagraphNumber()
Dim para As Paragraph
Dim bookmarkName As String
Dim paragraphNum As String
            
    ' 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.ListString
           
            ' Generate a bookmark name with the first four characters of the paragraph number
            bookmarkName = "Bookmark_" & paragraphNum
            'paragraphNumStr = Left(paragraphNumStr, InStr(1, paragraphNumStr, Chr(32)))
            bookmarkName = "Bookmark_" & Trim(Replace(paragraphNum, ".", "_"))
            ' Add a bookmark to the paragraph
            ActiveDocument.Bookmarks.Add bookmarkName, para.Range
        End If
    Next para
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Cross-reference with full context a numbered list inside another multilevel list (list style) MatLcq Word 0 02-01-2021 06:00 AM
Convert multilevel bullet list to multilevel numbered list scadaman29325 Word 2 03-27-2020 03:27 PM
Insert a bookmark for each paragraph in a multilevel list Find word, Insert Paragraph and bookmark gattaca714 Word VBA 2 03-24-2017 09:23 PM
Insert a bookmark for each paragraph in a multilevel list Insert Paragraph after each list item - All Lists dan88 Word VBA 6 05-07-2016 05:13 PM
Multilevel lists and paragraph indentation computerdummy Word 2 04-27-2012 02:32 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 02:39 PM.


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