vba 当使用 range.find 查找粗体文本时,它不会找到整个选择是否为粗体!

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/975033/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 10:28:54  来源:igfitidea点击:

When using range.find to find bold text it won't find if the entire selection is bold!

vbaword-vba

提问by Kevin

I'm trying to extract bold text using the range.find method and all is peachy except if the entire range is actually bold (not likely to happen much, it's more of an edge condition).

我正在尝试使用 range.find 方法提取粗体文本,除非整个范围实际上是粗体(不太可能发生,它更像是一种边缘条件),否则一切都是桃色的。

With rngFindRange.Find
.ClearFormatting
.Font.Bold = True
Do
    .Execute

    If Not .Found Then
         Exit Do
    End If

    'do something with found text'

    Set rngFindRange = ActiveDocument.Range(rngFindRange.End + 1, Selection.End)

Loop

The above matches bold text right at the start or right at the end, even both but not when the entire range is bold. I think I might have to test the range.font.bold = true before searching through the range. What does stackoverflow think?

以上匹配开头或结尾处的粗体文本,甚至两者都匹配,但当整个范围为粗体时不匹配。我想我可能必须在搜索范围之前测试 range.font.bold = true。stackoverflow 是怎么想的?

回答by guillermooo

This should find any bold text:

这应该找到任何粗体文本:

Sub SearchBoldText()
    Dim rng As Range
    Set rng = ThisDocument.Range(0, 0)
    With rng.Find
        .ClearFormatting
        .Format = True
        .Font.Bold = True
        While .Execute
            rng.Select
            rng.Collapse direction:=wdCollapseEnd
        Wend
    End With
    Set rng = Nothing
End Sub