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

    Finding Highlighted Text in VB (Word 2003)

    Hi,

    I have written the macro below to check the document to see if highlighted text exists. I had to add a workaround for the case where the highlighted text is in a table, since the macro seemed to go into an endless loop otherwise. Now though, I have an example where the macro doesn't work at all : it finds highlighted text even though there isn't any... even worse than that, it thinks that every character in the document is highlighted!! But when I look in the de###### at the contents of the found Selection, the attributes show correctly that it isn't highlighted.

    Maybe my Selection.Find.Execute isn't working.. so I tried it manually using the "Find" dialog in word, using the search attributes that were already set from the macro and naturally no highlighted text was found.

    Before I attach the offending document here, I'd be grateful if someone could cast an eye over the code.. I have probably done something really silly :-)

    ================================================== ================


    Public Function CheckHighlightedText() As VbTriState

    Call Logging.LogHeading(LoggingForm.txtResults, "Checking Highlighted Text", False)

    Dim Count As Integer
    Count = 0


    'search for highlighted text
    With Selection.Find
    .ClearFormatting
    .Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Format = True
    .Highlight = True
    End With

    'search through document for highlighted format
    Do While Selection.Find.Execute

    Count = Count + 1

    Call Logging.Log(LoggingForm.txtResults, "Highlighted Text found in Section " & _
    Selection.Information(wdActiveEndSectionNumber) & " on Page " & _
    Selection.Information(wdActiveEndAdjustedPageNumbe r) & _
    " Line " & Selection.Information(wdFirstCharacterLineNumber) & ". Text: '" & Selection.Text & "'.")

    If Selection.Information(wdWithInTable) Then
    'skip the table because it causes an endless loop!
    Call Logging.Log(LoggingForm.txtResults, "In Table, jumping out..")

    While Selection.Information(wdWithInTable)
    Selection.MoveDown
    Wend

    End If

    Loop

    Call Logging.Log(LoggingForm.txtResults, "A total of " & Count & " highlighted passages were found", True, False)

    Call Logging.LogDivider(LoggingForm.txtResults)

    'set the function return value to indicate success
    If Count > 0 Then
    CheckHighlightedText = vbFalse
    Else
    CheckHighlightedText = vbTrue
    End If

    ================================================== ================

    End Function

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

    Re: Finding Highlighted Text in VB (Word 2003)

    Actually, the problem only seems to be with this one single document. And now I have fixed it - but I don't know how it got into that state in the first place. The issue was that when i selected all the text in the document and then clicked on the "highlight" format toolbar button, the yellow highlight color was already selected.. yet visually you couldn't see anything. When searching for highlighted text using the Find dialog, it couldn't find anything - yet my macro thought that every character in the document was highlighted.

    Bizarre. Corrupt file maybe? Although I am surprised the macro works differently than the Find dialog.

    In any case, the matter was resolved by selecting all document text and choosing "No Highlight" from the highlight toolbar button, then running the macro again.

    cheers

  3. #3
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Finding Highlighted Text in VB (Word 2003)

    Strange. Thank you for posting the update.

    StuartR

Posting Permissions

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