Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 02-11-2020, 06:56 AM
irinarox irinarox is offline Macros update - change only highlighted terms throughout document Windows 10 Macros update - change only highlighted terms throughout document Office 2016
Novice
Macros update - change only highlighted terms throughout document
 
Join Date: May 2019
Posts: 9
irinarox is on a distinguished road
Default Macros update - change only highlighted terms throughout document

Hello,


I have a find-replace Macro which I am using for replacing text in Word documents. However, there are certain words which I need to be replaced if they are highlighted in yellow and left as is if they are not.



For example (2 of the USD mentions below should have yellow highlight on them, but it doesn't work here):
XXXXXXXX USD
XXXXXXXXXX USD
XXXXXXXXXXXX USD
XXXXXXXX USD
XXXXXXXX USD


The Macro I have is the following, but the problem is that it is replacing USD everywhere in the document. Is there a way to update it so it changes only the yellow highlighted USD?


Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = USD
.Replacement.Text = US Dollars

.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll




Thanks for the help with this!
Reply With Quote
  #2  
Old 02-11-2020, 08:09 AM
gmaxey gmaxey is offline Macros update - change only highlighted terms throughout document Windows 10 Macros update - change only highlighted terms throughout document Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,421
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

Code:
Sub ScratchMacro()
  'A basic Word macro coded by Greg Maxey
Dim oRng As Range
  Set oRng = Selection.Range
  With oRng.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "USD"
    .Replacement.Text = "US Dollars"
    .Highlight = True
    .Execute Replace:=wdReplaceAll
  End With
lbl_Exit:
  Exit Sub
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #3  
Old 02-11-2020, 08:20 AM
irinarox irinarox is offline Macros update - change only highlighted terms throughout document Windows 10 Macros update - change only highlighted terms throughout document Office 2016
Novice
Macros update - change only highlighted terms throughout document
 
Join Date: May 2019
Posts: 9
irinarox is on a distinguished road
Default

Thanks Greg! There is only one issue here - I was looking for a way in which only the yellow highlighted ones are changed by this Macro - for example if I have an USD text which is highlighted in green in the same document - then it should remain unchanged.
Reply With Quote
  #4  
Old 02-11-2020, 08:46 AM
gmaxey gmaxey is offline Macros update - change only highlighted terms throughout document Windows 10 Macros update - change only highlighted terms throughout document Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,421
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

Code:
Sub ScratchMacro()
  'A basic Word macro coded by Greg Maxey
Dim oRng As Range
  Set oRng = ActiveDocument.Range
  With oRng.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "USD"
    Do While .Execute
      If oRng.HighlightColorIndex = wdYellow Then
        oRng.Text = "U.S. Dollars"
      End If
      oRng.Collapse wdCollapseEnd
    Loop
  End With
lbl_Exit:
  Exit Sub
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #5  
Old 02-11-2020, 08:53 AM
irinarox irinarox is offline Macros update - change only highlighted terms throughout document Windows 10 Macros update - change only highlighted terms throughout document Office 2016
Novice
Macros update - change only highlighted terms throughout document
 
Join Date: May 2019
Posts: 9
irinarox is on a distinguished road
Default

Thank you so much for the help! It works great
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Macros update - change only highlighted terms throughout document VBA code to compile one document based on multiple search terms Hoxton118 Word VBA 4 04-04-2021 06:02 AM
Macros update - change only highlighted terms throughout document Backspace Highlighted Field in Protected Document ACDJFS Word 12 03-17-2020 03:37 PM
Macros update - change only highlighted terms throughout document Best Practice for Indexing Multiple Word Terms and Sub-Terms jhy001 Word 4 11-06-2017 02:08 PM
Macros update - change only highlighted terms throughout document Code to update macros automatically in startup folder dwirony Word VBA 4 01-12-2017 08:23 AM
Can you make templates dynamically update macros/etc. from Normal.DOT? New Daddy Word 2 11-18-2013 09:07 AM

Other Forums: Access Forums

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