Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 02-10-2014, 12:31 PM
hwg hwg is offline Find instance of a word in a specific style and change its color Windows 7 64bit Find instance of a word in a specific style and change its color Office 2010 64bit
Novice
Find instance of a word in a specific style and change its color
 
Join Date: Feb 2014
Posts: 8
hwg is on a distinguished road
Default Find instance of a word in a specific style and change its color

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
Reply With Quote
  #2  
Old 02-10-2014, 12:38 PM
gmaxey gmaxey is offline Find instance of a word in a specific style and change its color Windows 7 32bit Find instance of a word in a specific style and change its color Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,428
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #3  
Old 02-10-2014, 02:28 PM
macropod's Avatar
macropod macropod is offline Find instance of a word in a specific style and change its color Windows 7 32bit Find instance of a word in a specific style and change its color Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
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

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]
Reply With Quote
  #4  
Old 02-15-2014, 06:15 PM
hwg hwg is offline Find instance of a word in a specific style and change its color Windows 7 64bit Find instance of a word in a specific style and change its color Office 2010 64bit
Novice
Find instance of a word in a specific style and change its color
 
Join Date: Feb 2014
Posts: 8
hwg is on a distinguished road
Default

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
Reply With Quote
  #5  
Old 02-15-2014, 06:22 PM
hwg hwg is offline Find instance of a word in a specific style and change its color Windows 7 64bit Find instance of a word in a specific style and change its color Office 2010 64bit
Novice
Find instance of a word in a specific style and change its color
 
Join Date: Feb 2014
Posts: 8
hwg is on a distinguished road
Default

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
It gave me the error "Object Required" on the line .Style.Name = "... "
Reply With Quote
  #6  
Old 02-15-2014, 08:31 PM
macropod's Avatar
macropod macropod is offline Find instance of a word in a specific style and change its color Windows 7 32bit Find instance of a word in a specific style and change its color Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
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

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]
Reply With Quote
  #7  
Old 02-20-2014, 05:01 PM
hwg hwg is offline Find instance of a word in a specific style and change its color Windows 7 64bit Find instance of a word in a specific style and change its color Office 2010 64bit
Novice
Find instance of a word in a specific style and change its color
 
Join Date: Feb 2014
Posts: 8
hwg is on a distinguished road
Default

I changed

Code:
.Style.Name =
to

Code:
.Style =
and it stopped on the first .Style line, with the error: "Run-time error '5834': Item with specified name does not exist"
Reply With Quote
  #8  
Old 02-20-2014, 10:59 PM
macropod's Avatar
macropod macropod is offline Find instance of a word in a specific style and change its color Windows 7 32bit Find instance of a word in a specific style and change its color Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
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

Did you specify a known Style's name where the code has "<my style name here>"?
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Find instance of a word in a specific style and change its color Deleting A blank Line that has a specific heading style , word 2010 & 2013 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

Other Forums: Access Forums

All times are GMT -7. The time now is 06:45 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