永无止境...

时间:2019-08-09 08:28:39

标签: vba while-loop ms-word ms-office

我试图计算以黄色/绿色/灰色或蓝色突出显示的单词数。搜索部分运行良好,但是我注意到当表中最后一个突出显示的范围时,Do While ... Loop永远不会结束并使Word崩溃...知道为什么会发生这种情况吗?

我也尝试了While ... Wend,但同样的问题。而且仅针对这种情况!否则,整个代码可以正常工作。

'Find all highlighted text
With Selection.Find
    .ClearFormatting
    .Wrap = wdFindStop 'until end of doc
    .Highlight = True

'If highlight color is blue add to w_blueMatch, else add to w_yellowGreenGreyMatch
    Do While .Execute(Forward:=True)
        If .Found Then
            Set o_tempRange = Selection.Range
            If o_tempRange.Hyperlinks.count > 0 Then
                For Each o_link In o_tempRange.Hyperlinks
                    o_link.Delete
                Next o_link
            End If
            If o_tempRange.HighlightColorIndex = wdTurquoise Then
                w_blueMatch = w_blueMatch + o_tempRange.ComputeStatistics(wdStatisticWords)
            Else
                w_yellowGreenGreyMatch = w_yellowGreenGreyMatch + o_tempRange.ComputeStatistics(wdStatisticWords)
            End If
        End If
    Loop
End With

(我对超链接有同样的问题...这就是为什么我将它们删除的原因...) 谢谢!!!

0 个答案:

没有答案