通过VBA突出显示Word Doc中的所有书签

时间:2016-10-13 20:51:25

标签: vba ms-word highlight

我想突出显示Word文档中的所有书签。当我试图显示书签时,我只得到“我”。而且这段代码没有做任何事情。

就像其中一位评论员写的那样,我的书签长度为0。但即便如此,我怎么能突出说出前进2个空格呢?

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

1 个答案:

答案 0 :(得分:1)

如果您的书签范围为零,并且您仍想在文档中突出显示某些内容,则可以扩展书签范围,例如:是文档中的以下字符:

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