1

我创建了一个循环到文档末尾的搜索和替换宏,如下所示:

Sub CheckEnglishAndTypos()
    Do Until ActiveDocument.Bookmarks("\Sel").Range.End = ActiveDocument.Bookmarks("\EndOfDoc").Range.End
    'Loop the search till the end
        Selection.MoveDown Unit:=wdLine, Count:=1
        Selection.Paragraphs(1).Range.Select
        With Selection.Find
            .Text = "(<*>) \1"
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindStop
            .Format = True
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = True
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
            Selection.Find.Execute Replace:=wdReplaceAll
        Loop
        ' Searching the remaning (till the end of document)
        Exit Sub
End Sub

问题是,如果文档有任何脚注,并且搜索移动到脚注,它将给出“请求的集合成员不存在”错误。显然,如果选择/光标在脚注内,并且文档在脚注所在的页面后面有页面,则宏无法找到文档的结尾。

有没有办法修复它?从搜索中排除脚注的方法很酷,但我对任何其他替代解决方案持开放态度。

4

2 回答 2

0

试试这个,它应该做整个文件

Sub CheckEnglishAndTypos()

    Options.DefaultHighlightColorIndex = wdBlue
'   Options.DefaultHighlightColorIndex = wdYellow

    With ActiveDocument.Content.Find
        .ClearFormatting
        .Text = "(<*>) \1"
        .Replacement.Text = ""
        .Replacement.ClearFormatting
        .Replacement.Highlight = True
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchWildcards = True
        .Execute Replace:=wdReplaceAll
    End With
End Sub
于 2017-08-19T23:10:44.093 回答
0

搜索字符串是问题

Sub CheckEnglishAndTypos()

    Options.DefaultHighlightColorIndex = wdBlue
'   Options.DefaultHighlightColorIndex = wdYellow

    With ActiveDocument.Content.Find
        .ClearFormatting
'       .Text = "(<*>) \1"                ' really slow
        .Text = " ([A-Za-z]@) \1"
        .Replacement.Text = ""
        .Replacement.ClearFormatting
        .Replacement.Highlight = True
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchWildcards = True
        .Execute Replace:=wdReplaceAll
    End With
End Sub
于 2017-08-27T21:11:11.403 回答