#1
|
|||
|
|||
Change fonts of a defined word with VBA
Hi everyone, how can I modify with a VBA, the fonts (in fact, to change the color) of a defined word frequently used in a document. I tried to used the search/replace but for some reasons, it does not seems to work in a VBA. For instance: FQCN73 CWUL bla bla bla would become FQCN73 CWUL bla bla bla... Thank you Michael Last edited by Michael007; 01-31-2011 at 06:16 PM. Reason: use of proper term |
#2
|
||||
|
||||
Hi Michael,
You can certainly do this via Find/Replace and, if you activate the macro recorder while doing so, Word will generate the corresponding code.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
Change fonts of a defined word with VBA
Hi Paul,
I had already tried a few time to do that via the macro recorder. While I can modify with success the color of a word via Find/Replace, it seems that the code recorded contains no specific command in term of the new color. This is what I got from the operation where I changed "FQCN73 in red: Code:
Selection.Find.ClearFormatting Selection.Find.Replacement.ClearFormatting With Selection.Find .Text = "FQCN73" .Replacement.Text = "FQCN73" .Forward = True .Wrap = wdFindContinue .Format = True .MatchCase = False .MatchWholeWord = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False End With Selection.Find.Execute Replace:=wdReplaceAll Michael |
#4
|
||||
|
||||
Hi Michael,
Try: Code:
Sub ColourChange() With ActiveDocument.Content.Find .ClearFormatting .Text = "FQCN73" With .Replacement .ClearFormatting .Text = "^&" .Font.Color = wdColorRed End With .Forward = True .Wrap = wdFindContinue .Format = True .MatchCase = False .MatchWholeWord = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False .Execute Replace:=wdReplaceAll End With End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
Change fonts of a defined word with VBA
Paul,
it works fine. One last question, let's suppose that I have several words that I wish to change in red, should I repeat all the commands as many time as the number of words or there is short-cut to do that? Thank you Michael |
#6
|
||||
|
||||
Hi Michael,
You could keep adding: .Text = "Stringtofind" .Execute Replace:=wdReplaceAll expressions before the 'End With' line, or you could use code like: Code:
Sub ColourChange() Application.ScreenUpdating = False Dim arrWords, i As Long arrWords = Array("FQCN73", "2nd string", "3rd string") With ActiveDocument.Content.Find .ClearFormatting .Forward = True .Wrap = wdFindContinue .Format = True .MatchCase = True .MatchWholeWord = True .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False With .Replacement .ClearFormatting .Text = "^&" .Font.Color = wdColorRed End With For i = 0 To UBound(arrWords) .Text = arrWords(i) .Execute Replace:=wdReplaceAll Next End With Application.ScreenUpdating = True End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#7
|
|||
|
|||
Change fonts of a defined word with VBA
Paul,
That's nice of you to share all this information. Thank you Michael |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
"Change Word Options" don't change | ADKREV | Word | 0 | 11-12-2010 10:56 AM |
How to make styles defined in one document available to ALL other documents, period. | wornways | Word | 1 | 08-05-2010 09:09 PM |
Change Default Font Word | datuk.ahmad | Word | 3 | 03-09-2010 04:47 PM |
TOC printing Error Bookmark not Defined | techexpressinc | Word | 0 | 12-14-2008 05:24 PM |
Defined Views in Tasks | Inga | Office | 3 | 07-20-2005 07:10 AM |