Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #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: 22,344
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
 



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 07:57 AM.


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