Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #3  
Old 04-06-2021, 10:38 AM
Stephen Ray Stephen Ray is offline Highlight A Phrase In A Document, Then Find And Replace Something Only In The Highlighted Part? Windows 10 Highlight A Phrase In A Document, Then Find And Replace Something Only In The Highlighted Part? Office 2016
Advanced Beginner
Highlight A Phrase In A Document, Then Find And Replace Something Only In The Highlighted Part?
 
Join Date: Sep 2018
Location: Kansas
Posts: 34
Stephen Ray is on a distinguished road
Default

Thank you, Macropod,

This seems to be the key:
.Wrap = wdFindStop

Here's what I am going to use. This gives me the precision I want.
Previous to this code below, the Macro would Find "Organization:"
And highlight the remainder of line after the :colon.
This works if the highlighted part is at least one character longer than
"AFL of the CIO From Hawaii"
It will not find it if the highlighted part is exactly "AFL of the CIO From Hawaii"

Code:
With Selection.Find
    .Text = "AFL of the CIO From Hawaii"
    .ClearFormatting
    .Replacement.ClearFormatting
    .Wrap = wdFindStop
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
End With
Selection.Find.Execute
If Selection.Find.Found Then
       With Selection.Find
           .ClearFormatting
           .Replacement.ClearFormatting
           .Text = "CIO"
           .Replacement.ClearFormatting
           .Replacement.Text = " CIB "
           .Execute Replace:=wdReplaceAll, Forward:=True, _
            Wrap:=wdFindStop
           Selection.Find.Execute Replace:=wdReplaceAll
           'the above line will work with =wdReplaceone   Same Results.
           Selection.Copy

'       Selection.HomeKey Unit:=wdLine
'       Selection.TypeText "Found IT"
'Else
'       Selection.TypeText "Did Not Find IT"
'       Selection.HomeKey Unit:=wdLine
       End With
 End If
Amazingly, "AFL of the CIB From Hawaii" is highlighted after this runs.
Reply With Quote
 



Similar Threads
Thread Thread Starter Forum Replies Last Post
Highlight A Phrase In A Document, Then Find And Replace Something Only In The Highlighted Part? VBA Find&Replace all bold, itlaic, underlined and highlighted words/characters Kalü Word VBA 22 04-24-2018 05:35 AM
Highlight A Phrase In A Document, Then Find And Replace Something Only In The Highlighted Part? Find, select, and replace part of text with bold paik1002 Word VBA 4 12-07-2015 11:24 PM
Highlight A Phrase In A Document, Then Find And Replace Something Only In The Highlighted Part? Find-replace using part of what was found in the replacement text paulkaye Word 3 12-22-2014 02:52 AM
Highlight A Phrase In A Document, Then Find And Replace Something Only In The Highlighted Part? Find/Replace Wildcard Needed-Bold & Highlight rsrasc Word VBA 3 11-11-2014 03:55 PM
find - reading highlight - highlight all / highlight doesn't stick when saved bobk544 Word 3 04-15-2009 03:31 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 12:46 PM.


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