How do I convert shading to highlighting for specific colors

Joined
Feb 5, 2017
Messages
3
Reaction score
0
Hello,
I've been reading about different macros to try to convert shaded text to highlighted text. None has worked for my purposes. I have a lot of documents that have been incorrectly coded by some novice coders. They were supposed to use highlighting but they used shading. My question is this:
How do I convert shaded text of a specific color (i.e. red) to shaded text of the same specific color (i.e. red)?
After that, I would like to convert shaded text of another color (i.e. yellow) to highlighted text of the same specific color (i.e. yellow)?

After reading, it looks like you cannot use Find and Replace to identify shaded text of a specific color so I am requesting someone who is more versed in VBA to please write a Macro that I can use to go into every one of these documents and replace all the red shaded text with red highlighting and all the yellow shaded text with yellow highlighting.

THANK YOU! I am under a tight deadline for a project so I appreciate your help!
 

macropod

Microsoft MVP
Joined
Mar 2, 2012
Messages
578
Reaction score
50
There are limits to what a macro can do in this context. Highlighting has only 16 options. Shading has 2^16 options. That said, you might try:

Code:
Sub Demo()
Dim i As Long
Application.ScreenUpdating = False
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = ""
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Format = True
    .Replacement.Font.ColorIndex = wdAuto
    .Replacement.Highlight = True
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    For i = 1 To 16
      Options.DefaultHighlightColorIndex = i
      .Font.ColorIndex = i
      .Execute Replace:=wdReplaceAll
    Next
  End With
End With
Application.ScreenUpdating = True
End Sub
 
Joined
Feb 6, 2017
Messages
5
Reaction score
0
Thanks @macropod . But unfortunately the macro above did not work as it was expected to.

We are having trouble in selecting text of a particular shading color.

Using

char.Font.Shading.BackgroundPatternColor <> wdColorAutomatic

works, selecting characters of any shading other than automatic, but using

char.Font.Shading.BackgroundPatternColor = wdColorYellow

does not work. We basically want to be able to select text with a particular shading color. Any ideas as to why this isn't working or what might work rather?
 
Joined
Feb 5, 2017
Messages
3
Reaction score
0
Could someone please reply to this thread? Ananya and I are trying to figure this out without luck.
Thank you! Please see her post above.
 

macropod

Microsoft MVP
Joined
Mar 2, 2012
Messages
578
Reaction score
50
As I already said, a macro can't directly convert all possible shadings to a highlight. There are only 16 possible highlights. Ananya's post does not concern the same issue as Gradtrainer sought help on: Ananya wants to change a selected colour to an as-yet unspecified highlight; Gradtrainer wants to change colours without apparently selecting anything.

Ananya's complaint that the macro doesn't do what it was expected to shows only that Ananya expected it to do something other than what I said it would do. Ananya should start a separate thread instead of trying to hijack this one...

Gradtrainer: without more details of the colours used (i.e. their RGB values) and the highlights you want to replace them with, the macro is already doing as much as can be done.
 
Joined
Feb 5, 2017
Messages
3
Reaction score
0
Dear Macropod,

Thanks for your quick reply. First of all, it was our failure to properly communicate the situation. In fact, Ananya and I are working together on the same project. There's no hijacking going on, I assure you. I will try to explain more clearly our problem and to answer the questions that you raised.

Here is our situation. We have many Word documents that have been color coded by Person X. All the documents were supposed to be color coded using the highlighting function such that different colors would represent different types of words. However, Person X incorrectly coded many of these documents by using the shading function (rather than the highlighting function). We are now trying to find a way to change everything back to highlighting without having Person X go back and spend weeks re-coding everything by hand.

Here is what the documents currently look like. Within each document - some text is shaded in Color A (see conversion chart below), other text is shaded in Color B and other text is shaded in Color C. We are now trying to figure out how to - as Ananya said - select all text that was previously shaded Color A and then convert that specific text to be highlighted with a corresponding Color a.

Subsequently, within the same document, we want to then change all the specific text that was previously shaded Color B and replace it with text that is highlighted with a corresponding Color b. We want to replace all text shaded Color C with text highlighted with a corresponding Color c. And so on with Color D to Color d and Color E to Color e.

We have many, many documents. This is why we are seeking a macro - or some other ideas or ways - that we can convert all the specific text that is currently shaded a given color to text that is now highlighted that same given color.

To answer your question, here is the conversion chart for the colors. On the left are the shaded colors that Person X used and on the right are the equivalent highlighted colors that we would like to replace those shaded colors.

SHADED COLOR USED ----> HIGHLIGHTED COLOR DESIRED
Example: Color A ------> Color a
RGB(255,64,255) ----> Pink
RGB(0,252,255) ----> Turquoise
RGB(0,249,0) -----> Bright Green
RGB(254,251,0) -------> Yellow
RGB(4,50,255) ------> Blue

Let me know if that makes more sense or not and we can further clarify. I am attaching an example of some words that have been color coded in shading that need to be converted to highlighting.
 

Attachments

  • Example of error file.doc
    73 KB · Views: 219

macropod

Microsoft MVP
Joined
Mar 2, 2012
Messages
578
Reaction score
50
OK, to change shading to highlighting, instead of changing font colouring to highlighting (per the previous macro), two small changes are needed. Try:
Code:
Sub Demo()
Dim i As Long
Application.ScreenUpdating = False
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = ""
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Format = True
    .Replacement.Font.Shading.BackgroundPatternColorIndex = wdAuto
    .Replacement.Highlight = True
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    For i = 1 To 16
      Options.DefaultHighlightColorIndex = i
      .Font.Shading.BackgroundPatternColorIndex = i
      .Execute Replace:=wdReplaceAll
    Next
  End With
End With
Application.ScreenUpdating = True
End Sub
 
Joined
Feb 6, 2017
Messages
5
Reaction score
0
Hi @macropod

Thanks for your reply. Unfortunately, the macro above does not do anything in my sample file.

Could you please explain what text is replaced by the following line in your macro?

.Execute Replace:=wdReplaceAll

Thanks,
Ananya
 

macropod

Microsoft MVP
Joined
Mar 2, 2012
Messages
578
Reaction score
50
Ananya: how about you butt out of the discussion... The thread was started by Gradtrainer and he/she is the only person I'm interested in dealing with in this thread at this time. All you're doing is confusing things by raising different issues. You have a thread of your own - kindly stay there! The macro works fine if the shadings correspond with Word's ColorIndex values; if they don't, every possible shading has to be tested and processed on a case-by-case basis.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top