PDA

View Full Version : Word - Office 365: VBA wanted to find specific text & highlight in a given colour



Phil Smith
08-31-2014, 11:13 AM
Hi everyone.

I am currently trying Office 365 after using Word 2010.

Whilst I have dabbled in Excel VBA and can just about survive with some occasional help, I have absolutely no knowledge of Word VBA, so have come to plead my case for assistance here!

I have a document that changes twice or more a year which consists of a changing number of pages each time.

I am wondering if it is possible to use VBA to search the document to find the following text and then highlight that text in a given colour:

"67-" plus one unknown character - to be highlighted red

"68-" plus one unknown character - to be highlighted red

"165-" plus one unknown character - to be highlighted yellow

"168-" plus one unknown character - to be highlighted yellow

"172-" plus one unknown character - to be highlighted yellow

The unknown character will either be a 2,3,4,5,6,7,8 or 9

There will also be the occasional piece of text:

"Shunt Class 67, 172, 168 & 165" where I would like the "67" to be highlighted red and the "172", "168" & "165" highlighted yellow, but NOT the commas.

At some point, the last piece of text will also have "68" added to it, but I do not know where in relation to the text as it stands now. I would hazard a guess that if someone can come up with the VBA code to solve this problem for me, from reading the code, I might be able to change it to accommodate the extra text?

The quote marks are not used in the document. They are used here to delineate the actual text that needs to be searched for, from the text of my question.

Thanks in advance for your assistance!

Phil

macropod
08-31-2014, 03:04 PM
It's not clear from your post whether you have strings like '67-2' or '672'. In either case, you could use a wildcard Find/Replace, where:
• Assuming the former -
Find = <6[78]-[2-9]>
Replace = ^&
• Assuming the latter -
Find = <6[78][2-9]>
Replace = ^&
and you choose 'Highlight' as the replacement option and, before doing the Find/Replace, you set the highlight colour.

Similarly, for strings like '165-2' or '1652', you could use:
• Assuming the former -
Find = <16[58]-[2-9]>
Replace = ^&
Find = <172-[2-9]>
Replace = ^&
• Assuming the latter -
Find = <16[58][2-9]>
Replace = ^&
Find = <172[2-9]>
Replace = ^&

The individual numbers 67, 172, 168 & 165 can likewise be handled via:
Find = <67>
Replace = ^&
Find = <16[58]>
Replace = ^&
Find = <172>
Replace = ^&

No VBA required. Of course, all of the above could be rolled into a macro.

Phil Smith
08-31-2014, 03:55 PM
Hi

The reason I specifically requested VBA is because there would up to ten different find/replace searches to do in each document.

The first search would be for numbers 67, 68, 165, 168 & 172, each immediately followed by a hyphen which is then immediately followed by a single digit.

The second search would then follow just for 67, 68, 165, 168 or 172 with no other search text attached, purely those numbers only.

I also forgot to say that there are three documents issued each time meaning a total of thirty find/replace searches each time.

I hope that clarifies what I need to search for, although I think it is basically exactly what I originally requested.

macropod
08-31-2014, 05:20 PM
Using the macro recorder would produce code that does essentially what I described. It could be reduced to:

Sub Demo()
Application.ScreenUpdating = False
With ActiveDocument.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Wrap = wdFindContinue
.MatchWildcards = True
.Format = True
.Forward = True
.Highlight = False
.Replacement.Highlight = True
.Replacement.Text = "^&"
Options.DefaultHighlightColorIndex = wdRed
.Text = "<6[78]-[2-9]>"
.Execute Replace:=wdReplaceAll
.Text = "<6[78]>"
.Execute Replace:=wdReplaceAll
Options.DefaultHighlightColorIndex = wdYellow
.Text = "<16[58]-[2-9]>"
.Execute Replace:=wdReplaceAll
.Text = "<16[58]>"
.Execute Replace:=wdReplaceAll
.Text = "<172-[2-9]>"
.Execute Replace:=wdReplaceAll
.Text = "<172>"
.Execute Replace:=wdReplaceAll
End With
End With
Application.ScreenUpdating = True
End Sub

Phil Smith
09-01-2014, 03:50 AM
Thanks for that Paul. That worked a treat. :bow:

Having used it, I would also change the font colour of the red highlighted text from to white so as to make it slightly clearer.

When I turn on the macro recorder in Word, I can't seem to select any text to change the colour to enable me to see the recorded code.

If you could help out once more that would be appreciated.

macropod
09-01-2014, 05:34 AM
In that case, I'd swap the F/R order around, so there's only one font change in the code:

Sub Demo()
Application.ScreenUpdating = False
With ActiveDocument.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Wrap = wdFindContinue
.MatchWildcards = True
.Format = True
.Forward = True
.Highlight = False
.Replacement.Highlight = True
.Replacement.Text = "^&"
Options.DefaultHighlightColorIndex = wdYellow
.Text = "<16[58]-[2-9]>"
.Execute Replace:=wdReplaceAll
.Text = "<16[58]>"
.Execute Replace:=wdReplaceAll
.Text = "<172-[2-9]>"
.Execute Replace:=wdReplaceAll
.Text = "<172>"
.Execute Replace:=wdReplaceAll
.Replacement.Font.ColorIndex = wdWhite
Options.DefaultHighlightColorIndex = wdRed
.Text = "<6[78]-[2-9]>"
.Execute Replace:=wdReplaceAll
.Text = "<6[78]>"
.Execute Replace:=wdReplaceAll
End With
End With
Application.ScreenUpdating = True
End Sub
Of course, you might also consider using something other than wdRed for the highlight (e.g. wdPink, wdBrightGreen, wdTurquoise) and leaving the font colour alone.

Phil Smith
09-01-2014, 06:53 AM
The reason I need 67 highlighted in red is to make it stand out, almost as a warning, hence the red!

Not home at the moment, but I am looking forward to trying the code when I get home.

Thank you so much for your prompt help! :bow: