![]() |
#1
|
|||
|
|||
![]()
I have a number of Word documents that contain snippets of code that have "syntax highlighting" However, the syntax highlighter missed one of the keywords. I would like to write a macro to fix this.
I need to search through a document and find all instances of the word 'in' that are in Courier New font, and change their color to a custom color: RGB (255, 119, 0). It seems this should be simple for the VBA experts on this forum, but in trying it myself (as a complete newbie to VBA) , I have found navigating around the document to be rather confusing. If anyone here can point me in the right direction, it would be greatly appreciated. HWG |
#2
|
|||
|
|||
![]()
If it is just the main text area of the document you are concerned with then this should do:
Code:
Sub ScratchMacro() 'A basic Word macro coded by Greg Maxey Dim oRng As Word.Range Set oRng = ActiveDocument.Range With oRng.Find .Text = "in" .ClearFormatting .Font.Name = "Courier New" .Replacement.ClearFormatting .Replacement.Font.Color = RGB(255, 119, 0) .Execute Replace:=wdReplaceAll End With End Sub |
#3
|
||||
|
||||
![]()
FWIW, if you're using Character Styles for the colouring (preferred IMHO), you should apply the Style to the text, not simply change its colour.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#4
|
|||
|
|||
![]()
Thanks so much for the replies.
Is there a way I can have the macro ask the user for confirmation of each replacement? thanks, hwg |
#5
|
|||
|
|||
![]()
Also, I did try this using styles but couldn't get it to work. I must not have the right syntax to detect and replace on styles instead of fonts.
I tried something like this: Code:
Dim oRng As Word.Range Set oRng = ActiveDocument.Range With oRng.Find .Text = " in " .ClearFormatting .Style.Name = "<my style name here>" .Font.Size = 8 .Replacement.ClearFormatting .Replacement.Style.Name = "<my replacement style name here>" .Execute Replace:=wdReplaceAll End With |
#6
|
||||
|
||||
![]()
Instead of '.Style.Name = ' (twice) you should have '.Style = ' (twice)
Also, if the font size is correct for the Find Style, you don't need to specify it. As for the confirmation, you'll need a different approach. See, for example: https://www.msofficeforums.com/word-...html#post41805
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#7
|
|||
|
|||
![]()
I changed
Code:
.Style.Name = Code:
.Style = |
#8
|
||||
|
||||
![]()
Did you specify a known Style's name where the code has "<my style name here>"?
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
![]() |
Thread Tools | |
Display Modes | |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
![]() |
SteveWcg | Word | 5 | 01-08-2014 10:37 PM |
Can't change color of text in table style | ABode | Word | 6 | 12-03-2013 07:45 AM |
Find and replace multiple documents change style | BaPW | Word | 0 | 08-14-2011 11:12 AM |
How to find a specific word in Powerpoint? | Ozard80 | PowerPoint | 0 | 05-11-2011 04:42 PM |
Highlight text and find next instance | DrDOS | Word | 0 | 11-15-2010 04:02 PM |