none
Selection.Find infinite loop on Paragraph Mark RRS feed

  • Question

  • I have a 100+ page Word 2013 Document with many randomly highlighted words, sentences, paragraphs, sections.  I want to add a New Comment box to each highlight instance.  I hacked up the macro below using Macro Record, and suggestions from internet searches.  The While loop works great, adding a new Comment box to each highlight it finds, UNTIL it gets to a paragraph marker (as far as I can tell), and then just infinite loops at that marker adding a bazillion Comment boxes to the end of that paragraph, and not progressing through the document.  Any suggestions greatly appreciated.

    Sub AddNewCommentBoxes()
        Selection.Find.ClearFormatting
        Selection.Find.Highlight = True
        Options.DefaultHighlightColorIndex = wdYellow
        With Selection.Find
            .Text = ""
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindAsk
            .Format = True
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        While .Execute
            If .Found = True Then
                Selection.Comments.Add Range:=Selection.Range
                Selection.TypeText Text:="???"
                ActiveWindow.Panes(1).Activate     ' have to do this get get the context out of the

                                                                       'comment back into the document

            End If
        Wend
        End With
    End Sub

                                        

    • Edited by EliotRamey Wednesday, March 11, 2015 8:25 PM
    Wednesday, March 11, 2015 8:23 PM

Answers

  • Change

        MsgBox "Added " + count + " New Comments"

    to

        MsgBox "Added " & count & " New Comments"


    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    • Marked as answer by EliotRamey Thursday, March 12, 2015 4:49 PM
    Thursday, March 12, 2015 3:47 PM
  • I'm sorry, I can't reproduce your problem. Whether there is text after the last table in the document or not, the code finishes correctly...

    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    • Marked as answer by EliotRamey Thursday, March 12, 2015 5:46 PM
    Thursday, March 12, 2015 4:57 PM

All replies

  • Insert the following below ActiveWindow.Panes(1).Activate:

                Selection.MoveRight

    By the way, you can remove the lines

            If .Found = True Then

    and

            End If


    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    Wednesday, March 11, 2015 9:59 PM
  • Thanks.  That got me past one hurdle.  Now the macro hangs at the end of the document and I cant get it to stop.

    I've modified my code as follows.  Any suggestions greatly appreciated. Thanks in advance.

    Sub AddNewCommentBoxes()
        Dim count As Long
        count = 0
        Selection.Find.ClearFormatting
        Selection.Find.Highlight = True
        Options.DefaultHighlightColorIndex = wdYellow
        With Selection.Find
            .Text = ""
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindStop
            .Format = True
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        While .Execute
            If Selection.Information(wdWithInTable) <> True Then    ' dont want 100s of comments on one table
                Selection.Comments.Add Range:=Selection.Range
                Selection.TypeText Text:="???"
                ActiveWindow.Panes(1).Activate                      ' reset focus to away from Comment to Document

                Selection.MoveRight                                       'this got me past the end of pargraph.  Thanks!

    '            If Selection.MoveRight <> 1 Then Exit Sub      ' tried this to stop at end of document, it didnt work

                count = count + 1
            End If
        Wend
        End With
        MsgBox "Added " + count + " New Comments"
    End Sub


    • Edited by EliotRamey Thursday, March 12, 2015 3:22 PM
    Thursday, March 12, 2015 3:21 PM
  • Change

        MsgBox "Added " + count + " New Comments"

    to

        MsgBox "Added " & count & " New Comments"


    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    • Marked as answer by EliotRamey Thursday, March 12, 2015 4:49 PM
    Thursday, March 12, 2015 3:47 PM
  • Thanks!  That helped.  Still crashed at the end of file.   Turns out, the last item in the file is a table.  I put some random text after the table, that didnt work.  Highlighted it, and it DID!  So, while I've solved my initial problem with finishing the document, now I'm curious what may have caused the problem with not having any highlighted text after the final table?

    Thanks again for all the help.

    Thursday, March 12, 2015 4:49 PM
  • I'm sorry, I can't reproduce your problem. Whether there is text after the last table in the document or not, the code finishes correctly...

    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    • Marked as answer by EliotRamey Thursday, March 12, 2015 5:46 PM
    Thursday, March 12, 2015 4:57 PM
  • Thanks for trying.  Most appreciate all the help with getting through this little adventure. 
    Thursday, March 12, 2015 5:46 PM