Word 2016 VBA: 当光标位于脚注中时找不到 EndOfDoc
Word 2016 VBA: can't find EndOfDoc when cursor is in footnote
我制作了一个循环到文档末尾的搜索和替换宏,如下所示:
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 = "(<*>) "
.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
问题是,如果文档有任何脚注,并且搜索移动到脚注中,则会出现 "Requested Member of the Collection Does Not Exist" 错误。显然,如果 selection/cursor 在脚注内,宏无法找到文档的末尾,并且文档的页面跟在脚注所在的页面之后。
有办法解决吗?从搜索中排除脚注的方法会很酷,但我愿意接受任何其他替代解决方案。
试试这个,它应该可以完成整个文档
Sub CheckEnglishAndTypos()
Options.DefaultHighlightColorIndex = wdBlue
' Options.DefaultHighlightColorIndex = wdYellow
With ActiveDocument.Content.Find
.ClearFormatting
.Text = "(<*>) "
.Replacement.Text = ""
.Replacement.ClearFormatting
.Replacement.Highlight = True
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
End Sub
搜索字符串是问题所在
Sub CheckEnglishAndTypos()
Options.DefaultHighlightColorIndex = wdBlue
' Options.DefaultHighlightColorIndex = wdYellow
With ActiveDocument.Content.Find
.ClearFormatting
' .Text = "(<*>) " ' really slow
.Text = " ([A-Za-z]@) "
.Replacement.Text = ""
.Replacement.ClearFormatting
.Replacement.Highlight = True
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
End Sub
我制作了一个循环到文档末尾的搜索和替换宏,如下所示:
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 = "(<*>) "
.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
问题是,如果文档有任何脚注,并且搜索移动到脚注中,则会出现 "Requested Member of the Collection Does Not Exist" 错误。显然,如果 selection/cursor 在脚注内,宏无法找到文档的末尾,并且文档的页面跟在脚注所在的页面之后。
有办法解决吗?从搜索中排除脚注的方法会很酷,但我愿意接受任何其他替代解决方案。
试试这个,它应该可以完成整个文档
Sub CheckEnglishAndTypos()
Options.DefaultHighlightColorIndex = wdBlue
' Options.DefaultHighlightColorIndex = wdYellow
With ActiveDocument.Content.Find
.ClearFormatting
.Text = "(<*>) "
.Replacement.Text = ""
.Replacement.ClearFormatting
.Replacement.Highlight = True
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
End Sub
搜索字符串是问题所在
Sub CheckEnglishAndTypos()
Options.DefaultHighlightColorIndex = wdBlue
' Options.DefaultHighlightColorIndex = wdYellow
With ActiveDocument.Content.Find
.ClearFormatting
' .Text = "(<*>) " ' really slow
.Text = " ([A-Za-z]@) "
.Replacement.Text = ""
.Replacement.ClearFormatting
.Replacement.Highlight = True
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
End Sub