【问题标题】:Word 2016 VBA: can't find EndOfDoc when cursor is in footnoteWord 2016 VBA:当光标在脚注中时找不到 EndOfDoc
【发布时间】:2017-08-28 05:11:11
【问题描述】:

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

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

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

有什么办法解决吗?从搜索中排除脚注的方法很酷,但我愿意接受任何其他替代解决方案。

【问题讨论】:

  • 为什么sub的末尾有Exit Sub
  • 忘记了,哈哈。感谢您的通知。

标签: vba ms-word word-2016


【解决方案1】:

搜索字符串是问题

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

【讨论】:

    【解决方案2】:

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

    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
    

    【讨论】:

    • 我的文档通常有 300 多页,除非我按段落限制内容,否则需要一整天才能运行。
    • “按段落”是否更快?
    • 完成这项工作大约需要 1 小时,因此速度快了大约 24 倍。可能是因为它不必匹配相隔光年的单词
    • 我认为它会搜索双倍的单词
    • 从“高级查找”对话框开始搜索需要多长时间?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-25
    • 1970-01-01
    • 2018-10-02
    相关资源
    最近更新 更多