Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 04-05-2021, 11:03 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 Highlight A Phrase In A Document, Then Find And Replace Something Only In The Highlighted Part?

Is it possible to highlight a phrase in a document, then find and replace something only in the highlighted part?

I would be too embarrassed to tell you the contortions I have been through to accomplish this in some macros.

It has been a slow dawning for me that there has got to be a better way.

Very often, I copy parts from four online input documents, paste them to one Word Document and let my macro combine these four inputs into many phrases which I then must paste into a couple of other new online documents.

Since I never know which exact line the inputs are on, the macro must find “Organization:” for example:

Part of the doc:
Lower Echelon: AFL of the CIO
Sub Department: CIO
Organization: AFL of the CIO From Hawaii
Top Agency: AFL of the CIO Incorporated

Then once the “Organization:” is found, the macro could easily highlight "AFL of the CIO From Hawaii" by extending from the : colon to the end of the line.



How would you replace “CIO” with “CIB” only in the highlighted part?

Lastly, all the highlighted part with the changes, "AFL of the CIB From Hawaii" need to go to the clipboard somehow. I do it this way: Selection.Copy

I know how to find and replace things in the whole document or only on the 1st line using range alright, but not a highlighted part.

Thanks,
Reply With Quote
  #2  
Old 04-05-2021, 03:02 PM
macropod's Avatar
macropod macropod 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
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
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

For what you've described, you could use:
.Execute Replace:=wdReplaceOne
instead of:
.Execute Replace:=wdReplaceAll
and:
.Wrap = wdFindStop
instead of:
.Wrap = wdFindContinue

Beyond that, you could apply an InRange test. See: https://www.msofficeforums.com/151477-post6.html
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #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
  #4  
Old 04-06-2021, 01:48 PM
macropod's Avatar
macropod macropod 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
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
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

Simpler:
Code:
Sub Demo()
Dim Rng As Range
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    'Find from 'Organization:' to the end of the paragraph,
    ' but not including the paragraph break
    .Text = "Organization:[!^13]{1,}"
    .Replacement.Text = ""
    .Format = False
    .Forward = True
    .Wrap = wdFindStop
    .MatchWildcards = True
    .Execute
  End With
  If .Find.Found = True Then
    'Move the Start to ':',
    ' then a further two characters
    .MoveStartUntil ":", wdForward
    .Start = .Start + 2
    'Store the current range
    Set Rng = .Duplicate
    'Find & Replace 'CIO' in the current range
    With .Find
      .Text = "CIO"
      .Replacement.Text = "CIB"
      .Execute Replace:=wdReplaceOne
      'Report the outcome as it applies to the stored range
      If .Found = True Then
        Rng.Copy
        MsgBox """" & Rng.Text & """copied"
      Else
        MsgBox """" & Rng.Text & """NOT copied"
      End If
    End With
  End If
End With
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 04-06-2021, 02:59 PM
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

Macropod, Thanks for showing me.
But it is going to take me some studying to be able to figure out what and how it does it.
Perhaps I will learn it and be able to use it with something else, too.
Reply With Quote
  #6  
Old 04-06-2021, 03:07 PM
macropod's Avatar
macropod macropod 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
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
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

See the comments I've added to the code.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #7  
Old 04-07-2021, 05: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

Thanks Macropod. I mean to study this. I need to learn this notation, syntax or whatever you call it. I believe the ^ carrot means the first part of it.
Is this Regex? I looked at Regex to help me with PowerShell.
Reply With Quote
  #8  
Old 04-07-2021, 06:07 AM
macropod's Avatar
macropod macropod 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
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
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

As indicated in the code, it is performing a wildcard Find, for which see: Finding and replacing characters using wildcards

In [!^13]{1,}, the:
• [] pair says to find any instance of whatever is defined between them
• ! says to NOT find what follows
• ^13 represents ASCII 13, which is the first character of the ASCII 13 & 11 pair that together create a paragraph break
• {1,} says to continue finding such characters for as long as they go.

Hence:
.Text = "Organization:[!^13]{1,}"
tells Word to find 'Organization:' followed by any series of characters that do not include a paragraph break.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply



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 11:50 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