79292054

Date: 2024-12-18 18:01:14
Score: 0.5
Natty:
Report link

Your code is almost complete. Please give the revised script a try.

Microsoft documentation:

Characters.Last property (Word)

Paragraphs.Last property (Word)

Sub ContentsFrmBookmarksWLinks()
    Dim bookmark As bookmark, linkText As String
    
    With ActiveDocument
        ' Add a page break to separate from the existing content
        .Characters.Last.InsertBreak wdPageBreak

        ' Add a title to the new section
        .Paragraphs.Last.Range.Text = "List of Bookmarks:"

        ' Loop through all bookmarks in the document
        For Each bookmark In .Bookmarks
            .Characters.Last.InsertParagraphAfter
            ' Get the bookmark name
            linkText = bookmark.Name
            ' Create a clickable hyperlink to the bookmark
            .Hyperlinks.Add _
                Anchor:=.Paragraphs.Last.Range, _
                Address:="", _
                SubAddress:=linkText, _
                TextToDisplay:=linkText
        Next bookmark
    End With
    
    MsgBox "Bookmark list with links created."
End Sub


enter image description here

Reasons:
  • RegEx Blacklisted phrase (2.5): Please give
  • Probably link only (1):
  • Long answer (-0.5):
  • Has code block (-0.5):
  • High reputation (-2):
Posted by: taller