Search for specific coloured highlighted text

Robie
5StarLounger
Posts: 656
Joined: 18 Feb 2010, 14:26

Search for specific coloured highlighted text

Post by Robie »

I have received a rather large document (with more to come) with highlighted text of different colours. They have used nine (yes, nine) different highlights in the document and would like us to create sub-documents by removing text hiughlighted with certain colours. In the end, we have to product 4 different documents from this master document. It is a nightmare to go thru and is huge.

Two questions really.
1. Is it possible to search for particularly coloured highlighted text in Word using VBA, i.e. seach for 'yellow' highlighted text, 'green' highlighted text, etc?

2. Is it possible to setup *character* styles with highlighted text (obviously for the above)? That is taking the above and changing the text to be styles (so that I can search for styles rather than highlighted text and can delete/mark/etc. it). This would mean that I would be able to set specific words to specific styles.

Thanks.
Robie

User avatar
HansV
Administrator
Posts: 78457
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Search for specific coloured highlighted text

Post by HansV »

1. Unfortunately, you can only search for highlighted text with any color, not for highlighted text with a specific highlight color. This holds both for the interface and for VBA.

2. Highlighting cannot be part of a style definition (neither of a paragraph style, nor of a character style).

For these reasons, it might be preferable to use text shading instead of highlighting. You can find and replace shading, and include it in the definition of a style.
But if you have already received documents with highlighting, that won't help, of course.
Best wishes,
Hans

Robie
5StarLounger
Posts: 656
Joined: 18 Feb 2010, 14:26

Re: Search for specific coloured highlighted text

Post by Robie »

HansV wrote:1. Unfortunately, you can only search for highlighted text with any color, not for highlighted text with a specific highlight color. This holds both for the interface and for VBA.

2. Highlighting cannot be part of a style definition (neither of a paragraph style, nor of a character style).

For these reasons, it might be preferable to use text shading instead of highlighting. You can find and replace shading, and include it in the definition of a style.
But if you have already received documents with highlighting, that won't help, of course.
Thanks Hans.

Dang - that scuppers my plan.

Here is another thought. As you suggest, I can set up various styles with text shading (to match the highlights). Then for this document, I will go thru manually (god help me) finding each different highlight, remove highlight and then set the selected text to the appropriate character style set up earlier.

For future documents and further changes to this document itself, they can use the styles set up.
I hope that works.

User avatar
HansV
Administrator
Posts: 78457
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Search for specific coloured highlighted text

Post by HansV »

Perhaps you can use the following macro. It assumes that you have already set up character styles with the appropriate text shading. The macro finds each instance of highlighted text, assigns a character style and removes the highlighting.

Code: Select all

Sub ReplaceHighlight()
    Application.ScreenUpdating = False
    Selection.HomeKey Unit:=wdStory
    With Selection.Find
        .Text = ""
        .ClearFormatting
        .Format = True
        .Highlight = True
        Do While .Execute
            Select Case Selection.Range.HighlightColorIndex
                Case wdYellow
                    Selection.Style = "Yellow" ' use correct style name
                Case wdGreen
                    Selection.Style = "Green"
                Case wdRed
                    Selection.Style = "Red"
            End Select
            Selection.Range.HighlightColorIndex = wdNoHighlight
            Selection.Collapse Direction:=wdCollapseEnd
        Loop
    End With
    Application.ScreenUpdating = True
End Sub
Best wishes,
Hans

Robie
5StarLounger
Posts: 656
Joined: 18 Feb 2010, 14:26

Re: Search for specific coloured highlighted text

Post by Robie »

HansV wrote:Perhaps you can use the following macro. It assumes that you have already set up character styles with the appropriate text shading. The macro finds each instance of highlighted text, assigns a character style and removes the highlighting.

Code: Select all

Sub ReplaceHighlight()
    Application.ScreenUpdating = False
    Selection.HomeKey Unit:=wdStory
    With Selection.Find
        .Text = ""
        .ClearFormatting
        .Format = True
        .Highlight = True
        Do While .Execute
            Select Case Selection.Range.HighlightColorIndex
                Case wdYellow
                    Selection.Style = "Yellow" ' use correct style name
                Case wdGreen
                    Selection.Style = "Green"
                Case wdRed
                    Selection.Style = "Red"
            End Select
            Selection.Range.HighlightColorIndex = wdNoHighlight
            Selection.Collapse Direction:=wdCollapseEnd
        Loop
    End With
    Application.ScreenUpdating = True
End Sub
That is just perfect Hans - exactly what I was looking for (I should have phrased my question bit better). :clapping: :fanfare: :grin:

A BIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIG Thanks Hans. You have just saved me hours and hours of work.