• VBA to search for highlighted text – problem with Tables

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » VBA to search for highlighted text – problem with Tables

    Author
    Topic
    #485024

    Hi.. I’ve been struggling with this problem for a while. I have the following simple code to search for highlighted text in a document and to report each instance to a user:

    Code:
        
       With Selection.Find
            .Highlight = True
        End With
        
        
        ‘search through document for highlighted format
        Do While Selection.Find.Execute = True
     
            ‘DO SOMETHING
               
        Loop
    

    The problem comes when there is highlighted text in tables. If the Find.Execute finds some highlighted text in a table, it will keep finding the next highlight in the table until it reaches the last highlighted text in the table. If this is the last highlighted text in the document, the code enters an INFINITE LOOP.

    If there is some other highlighted text in the document after the table, then the search continues and then ends correctly.

    So the problem is only when the last highlighted text in the document is in a table. I had to add some code like:

    Code:
            If Selection.Information(wdWithInTable) Then
               ‘skip the table because it causes an endless loop!
                 While Selection.Information(wdWithInTable)
                     Selection.MoveDown
                Wend        
          End If
    

    But of course this skips over some of the text that i want to report.

    It’s driving me mad. CAn you help?

    Viewing 3 reply threads
    Author
    Replies
    • #1346951

      Hi Dom,

      Without seeing more of your code, it’s hard to say what the issue is.

      Cheers,
      Paul Edstein
      [Fmr MS MVP - Word]

    • #1346981

      Dom,

      I’m not real familiar with Word VBA but from a pure logic point of view you could try capturing information in a set of variables e.g,
      In Table Yes/No
      Value Found
      Table Coordinates

      Then on the next find compare against the saved values and if they are identical then execute the code to get out of the table. Of course you do have to find a set of values/properties that will always be unique to make this approach work.

      When you exit the table don’t forget to clear the In Table variable.:cheers:

      May the Forces of good computing be with you!

      RG

      PowerShell & VBA Rule!
      Computer Specs

    • #1347180

      Hi guys,
      thanks for the answers.

      Macropod: There is no more code to show, except perhaps a line at the start to set the Selection to the beginning of the document or region to be searched. If you replaced the “DO SOMETHING” with just a MsgBox to show the selected/found text, then you’d have the exact code which gets stuck in an infinite loop whenever the last highlighted text in the document is in a table.

      The funny thing is, if I just do the search using Advanced Find->Format->Highlight, it works fine (of course) and if I record it as a macro I get the same VBA code I produced above, but this doesn’t work..

      This only seems to fail if I’m looking for highlighted formatting – any other kind of search doesn’t get stuck in an infinite loop (I’ll go and check that).

      So there shouldn’t be anything wrong with my method (it’s very basic) and there is no more context…

      • #1347192

        Something is screwy here. I tried your exact code in 2007, with a counter variable being incremented and a msgbox showing the counter replacing Do Something.

        It finds the first instance of highlighted text (not in a table but in a heading) twice. THEN it goes on. If the last highlighted text is in a table, all is well. If it is not, the code loops indefinitely. I realize this is the opposite of your experience.

        The only odd thing about the first chunk of text it finds is that it was highlighted in two separate operations. Perhaps Word stores the beginning and end of a highlighted chunk when you create the highlight.

        Also, you may be getting strange results if you click the OK button on the message box and then press F8 to step through the macro. If the F8 is caught by the Word doc, you end up extending the selection. If the F8 is caught by the macro interpreter all is well. You could avoid the problem by replacing the msgbox with something that displayed a message briefly then disappeared without user intervention.

        I hope some of this helps.

        – Jessica

    • #1347307

      Try something based on:

      Code:
      Sub Demo()
      Dim i As Integer
      With ActiveDocument.Range
        With .Find
          .ClearFormatting
          .Replacement.ClearFormatting
          .Text = ""
          .Replacement.Text = ""
          .Forward = True
          .Highlight = True
          .Wrap = wdFindStop
          .Format = True
          .MatchCase = False
          .MatchWholeWord = False
          .MatchWildcards = False
          .MatchSoundsLike = False
          .MatchAllWordForms = False
          .Execute
        End With
        Do While .Find.Found
          i = i + 1
          MsgBox .Duplicate.Text
          .End = .End + 1
          .Collapse wdCollapseEnd
          .Find.Execute
        Loop
      End With
      MsgBox i & " highlighted ranges found."
      End Sub

      Cheers,
      Paul Edstein
      [Fmr MS MVP - Word]

    Viewing 3 reply threads
    Reply To: VBA to search for highlighted text – problem with Tables

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: