Results 1 to 13 of 13
  1. #1
    Lounger
    Join Date
    May 2002
    Location
    Cleveland, Ohio, USA
    Posts
    34
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Find Highlighting by Colors (Word XP SR-2)

    Howdy
    Is there anyway that I can use Word's Find and Replace feature to locate text by the specific color of the highlighting? I know it's easy to find highlighted text, but I need to find certain highlighted text based on the actual color of the highlighting.
    Smiles
    Kay
    <img src=/S/confused.gif border=0 alt=confused width=15 height=20>

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Find Highlighting by Colors (Word XP SR-2)

    I think you need a macro for this. Here is some rough code that finds yellow highlighting; you'll need to adapt it to your needs.

    Sub FindYellowHighlight()
    With Selection.Find
    .ClearFormatting
    .Highlight = True
    .Text = ""
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Format = True
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    .Execute
    Do Until Selection.Range.HighlightColorIndex = wdYellow Or .Found = False
    .Execute
    Loop
    End With
    End Sub

  3. #3
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Los Angeles Area, California, USA
    Posts
    7,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Find Highlighting by Colors (Word XP SR-2)

    Hans:
    This is great. Can I ask you to expand, please? How would I use the macro to replace all yellow highlighting in a document with a particular style? i.e. I would replace yellow highlighting with a style that had yellow shading, blue highlighting with blue shading, etc. This would be worth it, even if I had to run the macro several times, once for each color.

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Find Highlighting by Colors (Word XP SR-2)

    Something like this? The procedure ReplaceHighlight takes two arguments: a colorindex (wdYellow, wdRed, ...) and the name of the paragraph style to replace the highlighting.

    Sub ReplaceHighlight(HighlightColor As WdColorIndex, ReplaceStyle As String)
    On Error Goto ErrHandler
    Application.ScreenUpdating = False
    With Selection.Find
    .ClearFormatting
    .Highlight = True
    .Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Format = True
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    .Execute
    Do
    If .Found = False Then Goto ExitHandler
    If Selection.Range.HighlightColorIndex = HighlightColor Then
    Selection.Range.HighlightColorIndex = wdNoHighlight
    Selection.Paragraphs.Style = ReplaceStyle
    End If
    .Execute
    Loop
    End With
    ExitHandler:
    Application.ScreenUpdating = True
    Exit Sub
    ErrHandler:
    MsgBox Err.Description, vbExclamation
    Resume ExitHandler
    End Sub

    Call it like this:

    ReplaceHighlight wdRed, "RedStyle"
    ReplaceHighlight wdYellow, "YellowStyle"

    I have tested this only on simple documents; I can't guarantee it will work okay in complicated documents, so please save your work before trying it.

  5. #5
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Los Angeles Area, California, USA
    Posts
    7,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Find Highlighting by Colors (Word XP SR-2)

    Hi Hans:
    Thanks so much for the procedure. I'm having a little trouble with it, though.
    1. The line Sub ReplaceHighlight(HighlightColor As WdColorIndex, ReplaceStyle As String) doesn't show up in the Macros dialog box. I assume that's because of the wording between the parentheses. Can this work without anything between the parenthesis?

    2. I'm not sure what you mean by call it with... I seem to be getting variable not defined messages.
    3. My styles are character styles, not paragraph styles. I'm not sure if that makes a difference.

    In your macro, e.g. I've replaced HighlightColor with wdYellow (for that particular highlight color) & have a character style called Yellow also. So I've replaced the word ReplaceStyle with Yellow. I either get a message of variable not defined or if I put in

    Dim Yellow as wdStyleType

    I get invalid use of style type.

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Find Highlighting by Colors (Word XP SR-2)

    Hi Phil,

    The procedure doesn't turn up in your macro list because it has arguments. A macro is a Sub procedure without arguments. I think it is a good idea to keep it general instead of hard coding color and style. But you can create a macro that will turn up in the macro list as follows:

    Sub ReplaceYellow()
    ReplaceHighlight wdYellow, "Yellow"
    End Sub

    Note that the second argument to ReplaceHighlight is a string: it is the name of a style.

    I thought you were trying to replace highlighting by background shading; this can only be done with paragraph styles, not with character styles. If you want to apply a style only to selected text, replace Selection.Paragraphs.Style = ReplaceStyle by Selection.Style = ReplaceStyle.

    I tested this on Word 2002 SP-2. I don't have access to Word 97 at the moment (and to Word 2000 not at all).

    An alternative to creating a small macro as above would be to remove the arguments, and to prompt the user for them using InputBox. Downside is that you can't use wdYellow etcetera, you would either have to know the numeric equivalents (wdRed = 6, wdYellow = 7, etc.), or enter the name of the color and translate it to a color constant using a Select Case statement.

    Here is an example using input boxes (you'd need to finish the Select Case statement):

    Sub ReplaceHighlight2()
    Dim HighlightColor As WdColorIndex
    Dim ColorName As String
    Dim ReplaceStyle As String
    On Error GoTo ErrHandler
    ColorName = InputBox("Which color?", "Replace", "yellow")
    If ColorName = "" Then Exit Sub
    Select Case ColorName
    Case "yellow"
    HighlightColor = wdYellow
    Case "red"
    HighlightColor = wdRed
    ' etc.
    Case Else
    Exit Sub
    End Select
    ReplaceStyle = InputBox("Which style?", "By", "Normal")
    If ReplaceStyle = "" Then Exit Sub
    Application.ScreenUpdating = False
    With Selection.Find
    .ClearFormatting
    .Highlight = True
    .Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Format = True
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    .Execute
    Do
    If .Found = False Then GoTo ExitHandler
    If Selection.Range.HighlightColorIndex = HighlightColor Then
    Selection.Range.HighlightColorIndex = wdNoHighlight
    Selection.Style = ReplaceStyle
    End If
    .Execute
    Loop
    End With
    ExitHandler:
    Application.ScreenUpdating = True
    Exit Sub
    ErrHandler:
    MsgBox Err.Description, vbExclamation
    Resume ExitHandler
    End Sub

  7. #7
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Los Angeles Area, California, USA
    Posts
    7,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Find Highlighting by Colors (Word XP SR-2)

    Hi Hans:
    Thanks again for all of this. Sorry for the confusion. I'm still having trouble. What I'm trying to do is replace various highlighted text throughout a document, with a character style that has the background shading. That way I can later search the document using Find/Replace for the character styles. It actually occurs to me as I'm writing this that I might have just been better off with macros that searched for a particular highlight color.

    Nevertheless, after reading your post, I tried both methods. With the first method (the short macro), apparently Word 2000 doesn't support ReplaceHighlight. When I selected it & pressed F1, Help said "keyword not found".

    With the Case Select method, it only replaced the first instance of yellow highlighted text & then exited the sub. So I'm not sure what I'm doing wrong.

  8. #8
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Find Highlighting by Colors (Word XP SR-2)

    Phil,

    ReplaceHighlight is the name of the procedure (sub) in one of my earlier replies, it is not a built-in procedure. The "short macro" only works if the ReplaceHighlight procedure is present too.

    But I have to apologize: it seemed to work, but now it doesn't for me either. What <img src=/S/scratch.gif border=0 alt=scratch width=25 height=29> me is that the code works correctly if I single-step through it, but not if I run it - it replaces just one or two instances. I will try to find what's wrong, but if others have bright suggestions or better ideas, they are very welcome.

  9. #9
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Find Highlighting by Colors (Word XP SR-2)

    Phil,

    As I indicated in my previous reply, my code was flawed. Collapsing the selection before continuing the search seems to correct it. I have also built in a test to check whether the style to be applied is a paragraph style.

    I have attached a zipped document with the code, and also a paragraph style Yellow and a character style RedText to enable testing. Please let me know if itworks, and if not, what goes wrong.
    Attached Files Attached Files

  10. #10
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Find Highlighting by Colors (Word XP SR-2)

    > An alternative to creating a small macro as above would be to remove the arguments, and to prompt the user for them
    > using InputBox. Downside is that you can't use wdYellow etcetera, you would either have to know the numeric
    > equivalents (wdRed = 6, wdYellow = 7, etc.), or enter the name of the color and translate it to a color constant
    > using a Select Case statement.

    I have seen a trick that might work for this: it let you pass a variable name through another variable name. But would it let you pass a constant? And is there any hope I will ever remember where I saw it?

    (There actually was a related thread on the VB board some time back where someone wanted to pass the contents of a textbox on a userform into a mathematical equation. The answer might have been there, or might have been in a random article I read somewhere...)

  11. #11
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Los Angeles Area, California, USA
    Posts
    7,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Find Highlighting by Colors (Word XP SR-2)

    Hi Hans:
    Thanks for all the work. I really appreciate it. It pretty much works. Of course, if I use the paragraph style, it gives the whole paragraph a background shading, which is not what I want. I changed the RedText style to have a text shading instead of a font color change and it usually works. The only time it doesn't is if there are two different highlight colors next to each other & the space in between is highlighted.

    I have to play with it a little more to see if I can change this behavior. I'm sure that I have sentences in one color with just a word in a different color for emphasis. Perhaps it only needs a cursor move one character. Actually, what I'm thinking of is having three macros with 3 different colors so I can just press a button & avoid using an imput box. What do you think?

  12. #12
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Find Highlighting by Colors (Word XP SR-2)

    Hi Phil,

    The problem was that if contiguous ranges are highlighted with different colors, the Find method finds the entire highlighted range; Selection.Range.HighlightColorIndex returns 9999999 in that case.

    I have tried to work around this by testing for a HighlightColorIndex of 9999999 and looping through the characters if necessary. This will be slow for large documents.

    I have attached the new version. There is just one macro this time; it replaces yellow, red and green highlighting by character styles with a corresponding text shading. The usual caveats apply.
    Attached Files Attached Files

  13. #13
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Los Angeles Area, California, USA
    Posts
    7,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Find Highlighting by Colors (Word XP SR-2)

    Hi Hans:
    Works great! <img src=/S/bow.gif border=0 alt=bow width=15 height=15> Thank you so much.

    I also learned something new about TOCs. If you apply direct formatting or a character style to a heading style (including highlighting),it will show up in the TOC. (That I knew before.) However, if the direct formatting or character style is shading, it will not show up in the TOC.

Posting Permissions

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