Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-31-2011, 06:03 PM
Michael007 Michael007 is offline Change fonts of a defined word with VBA Windows 7 64bit Change fonts of a defined word with VBA Office 2003
Novice
Change fonts of a defined word with VBA
 
Join Date: Dec 2010
Posts: 18
Michael007 is on a distinguished road
Default 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
Reply With Quote
  #2  
Old 01-31-2011, 08:15 PM
macropod's Avatar
macropod macropod is offline Change fonts of a defined word with VBA Windows 7 32bit Change fonts of a defined word with VBA Office 2000
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #3  
Old 01-31-2011, 10:54 PM
Michael007 Michael007 is offline Change fonts of a defined word with VBA Windows 7 64bit Change fonts of a defined word with VBA Office 2003
Novice
Change fonts of a defined word with VBA
 
Join Date: Dec 2010
Posts: 18
Michael007 is on a distinguished road
Default 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
Reply With Quote
  #4  
Old 02-01-2011, 01:49 AM
macropod's Avatar
macropod macropod is offline Change fonts of a defined word with VBA Windows 7 32bit Change fonts of a defined word with VBA Office 2000
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #5  
Old 02-01-2011, 01:57 PM
Michael007 Michael007 is offline Change fonts of a defined word with VBA Windows 7 64bit Change fonts of a defined word with VBA Office 2003
Novice
Change fonts of a defined word with VBA
 
Join Date: Dec 2010
Posts: 18
Michael007 is on a distinguished road
Default 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
Reply With Quote
  #6  
Old 02-01-2011, 02:11 PM
macropod's Avatar
macropod macropod is offline Change fonts of a defined word with VBA Windows 7 32bit Change fonts of a defined word with VBA Office 2000
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #7  
Old 02-01-2011, 04:52 PM
Michael007 Michael007 is offline Change fonts of a defined word with VBA Windows 7 64bit Change fonts of a defined word with VBA Office 2003
Novice
Change fonts of a defined word with VBA
 
Join Date: Dec 2010
Posts: 18
Michael007 is on a distinguished road
Thumbs up Change fonts of a defined word with VBA

Paul,

That's nice of you to share all this information.

Thank you
Michael
Reply With Quote
Reply

Thread Tools
Display Modes


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
Change fonts of a defined word with VBA Defined Views in Tasks Inga Office 3 07-20-2005 07:10 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 01:35 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft