简体   繁体   中英

Highlight All Bookmarks in Word Doc Via VBA

I want to highlight all the bookmarks in my Word document. When I try to show the bookmarks, I only get the "I". And this code doesn't do anything.

Just like one of the commentators wrote, my bookmarks are 0 length. But even then how can I highlight say 2 spaces forward?

Sub BookMarks2Bold()
Dim bm As Bookmark
Dim tx As Range

Set tx = ActiveDocument.StoryRanges(wdMainTextStory)
For Each bm In tx.Bookmarks

    bm.Range.HighlightColorIndex = wdYellow
Next

End Sub

If your bookmarks are zero range and you still want to highlight something in the document, you can extend the bookmark range, eg be the following character in the document:

Sub BookMarks2Bold()
    Dim bm As Bookmark
    Dim tx As Range
    dim rng as Range

    Set tx = ActiveDocument.StoryRanges(wdMainTextStory)
    For Each bm In tx.Bookmarks
        set rng = bm.Range
        rng.MoveEnd wdCharacter ' extend by one character

        ' optionally, expand by one word
        ' rng.Expand wdWord

        rng.HighlightColorIndex = wdYellow
    Next

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM