View Single Post
  #1   Report Post  
Posted to microsoft.public.word.docmanagement
[email protected] o0aiur0o@gmail.com is offline
external usenet poster
 
Posts: 1
Default Macro can't find the end of document in word 2016 (Requested Memberof the Collection Does Not Exist)

I have a macro that can find all duplicate word pairs, highlight them, then play a sound to notify me as follow:

Sub CheckEnglishAndTypos()
'Go to the beginning to start the search
Selection.HomeKey Unit:=wdStory

Selection.Paragraphs(1).Range.Select
Selection.Find.Highlight = False
Selection.Find.Replacement.Highlight = True
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

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
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
PlayTheSound "Task Completed.wav"
End Sub

This thing works like a charm on short documents, but for anything over 100 pages, it gives me the "Requested Member of the Collection Does Not Exist".

In debug mode, the line corresponds to the error is "Do Until ActiveDocument.Bookmarks("\Sel").Range.End = ActiveDocument.Bookmarks("\EndOfDoc").Range.End".

When I checked over the test document, apparently the thing did highlight everything to the end. But for some bloody reason, it couldn't find where the document end. So the macro just stopped, and no sound was played.

Is there anyway to fix it?