Consulting

Results 1 to 6 of 6

Thread: Word 2016 VBA: can't find EndOfDoc when cursor is in footnote

  1. #1

    Word 2016 VBA: can't find EndOfDoc when cursor is in footnote

    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.

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    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
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    Quote Originally Posted by macropod View Post
    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.

  4. #4
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Quote Originally Posted by vkhu View Post
    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...
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  5. #5
    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).

  6. #6
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    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"
    Last edited by macropod; 08-20-2017 at 09:55 PM.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •