Log in

View Full Version : [SOLVED:] Word 2016 VBA: can't find EndOfDoc when cursor is in footnote



vkhu
08-19-2017, 11:38 AM
I made a search-&-replace macro that loops to the end of the document, like so:



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


Problem is, if the document has any footnote, and the search move into the footnote, it will then give a "Requested Member of the Collection Does Not Exist" error. Apparently, the macro can't find the end of the document if the selection/cursor is inside a footnote, and the document has pages following the page the footnote's at.


Is there anyway to fix it? A way to exclude the footnotes from search would be cool, but I'm open to any other alternative solutions.

macropod
08-19-2017, 03:24 PM
Why are you bothering with a loop? Try:

Sub CheckEnglishAndTypos()
With ActiveDocument.StoryRanges(Selection.StoryType).Find
.Text = "(<*>) \1"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=wdReplaceAll
End With
End Sub

vkhu
08-19-2017, 07:54 PM
Why are you bothering with a loop? Try:

Sub CheckEnglishAndTypos()
With ActiveDocument.StoryRanges(Selection.StoryType).Find
.Text = "(<*>) \1"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=wdReplaceAll
End With
End Sub
I tested it without loop before. The bloody thing took nearly a day to finish, and sometimes it highlighted stuff that are paragraphs apart! That's why I had to force it to select each paragraph individually, and do a search within the selection before moving on to the next one.

macropod
08-19-2017, 11:02 PM
I tested it without loop before. The bloody thing took nearly a day to finish, and sometimes it highlighted stuff that are paragraphs apart! That's why I had to force it to select each paragraph individually, and do a search within the selection before moving on to the next one.
But have you actually tested the code I posted??? In any event, you might consider using a Find expression that's better suited to what you're trying to do than the one you're now using...

vkhu
08-20-2017, 07:42 PM
Sorry for the late reply, but I tried it. Your code ran the whole night and the entire morning, yet it still couldn't finish the search. It probably works well on short documents, but mine are usually 200+ pages (I'm a translator).

macropod
08-20-2017, 08:46 PM
As I said, though, you might consider using a Find expression that's better suited to what you're trying to do than the one you're now using. For example:
.Text = "(<[! ^s]{1,}>) \1"