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

老子叫甜甜 提交于 2019-12-20 03:20:32

问题


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).

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?


回答1:


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


来源:https://stackoverflow.com/questions/975033/when-using-range-find-to-find-bold-text-it-wont-find-if-the-entire-selection-is

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!