|
|
Thread Tools | Display Modes |
#1
|
|||
|
|||
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, |
#2
|
||||
|
||||
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] |
#3
|
|||
|
|||
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 |
#4
|
||||
|
||||
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] |
#5
|
|||
|
|||
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. |
#6
|
||||
|
||||
See the comments I've added to the code.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#7
|
|||
|
|||
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. |
#8
|
||||
|
||||
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] |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
VBA Find&Replace all bold, itlaic, underlined and highlighted words/characters | Kalü | Word VBA | 22 | 04-24-2018 05:35 AM |
Find, select, and replace part of text with bold | paik1002 | Word VBA | 4 | 12-07-2015 11:24 PM |
Find-replace using part of what was found in the replacement text | paulkaye | Word | 3 | 12-22-2014 02:52 AM |
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 |