Results 1 to 6 of 6
  1. #1
    2 Star Lounger
    Join Date
    Oct 2007
    Posts
    172
    Thanks
    0
    Thanked 2 Times in 1 Post

    Unhappy VBA to search for highlighted text - problem with Tables

    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?

  2. #2
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,054
    Thanks
    2
    Thanked 417 Times in 346 Posts
    Hi Dom,

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

    Paul Edstein
    [MS MVP - Word]

  3. #3
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,434
    Thanks
    372
    Thanked 1,457 Times in 1,326 Posts
    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.
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  4. #4
    2 Star Lounger
    Join Date
    Oct 2007
    Posts
    172
    Thanks
    0
    Thanked 2 Times in 1 Post
    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...

  5. #5
    Star Lounger
    Join Date
    Feb 2011
    Location
    Washington, DC
    Posts
    98
    Thanks
    8
    Thanked 6 Times in 6 Posts
    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

  6. #6
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,054
    Thanks
    2
    Thanked 417 Times in 346 Posts
    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
    [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
  •