Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #2  
Old 10-08-2021, 09:23 PM
gmayor's Avatar
gmayor gmayor is offline Applying a VBA code only on a selected text or range Windows 10 Applying a VBA code only on a selected text or range Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,137
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

That's fairly straightforward and I assume you know that you have spelled 'John' incorrectly?
You only need to change the line
Code:
.Wrap = wdFindContinue
to
Code:
.Wrap = wdFindStop
however
Code:
Sub ReplaceName()
Dim oRng As Range
    If Len(Selection.Range) = 0 Then
        MsgBox "Select the text first", vbCritical
        Exit Sub
    End If
    Set oRng = Selection.Range
    With oRng.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = "Jhon"
        .Replacement.Text = "Robert"
        .Forward = True
        .Wrap = wdFindStop
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchKashida = False
        .MatchDiacritics = False
        .MatchAlefHamza = False
        .MatchControl = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        .Execute Replace:=wdReplaceAll
    End With
    Set oRng = Nothing
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
 

Tags
find & replace, vba, word vba



Similar Threads
Thread Thread Starter Forum Replies Last Post
Vertically Center Selected Range in the Useable Window gmaxey Word VBA 0 12-19-2020 10:01 AM
Applying a VBA code only on a selected text or range Find and Replace Selected Text or Macro for finding selected text mrplastic Word VBA 4 12-20-2019 01:25 PM
Applying a VBA code only on a selected text or range Set Range based on selected rows. 14spar15 Excel Programming 8 11-19-2018 08:08 AM
Two problems: Applying bold affects other tables and canvas doesn't appear at specified range slaycock Word VBA 6 12-08-2016 02:28 AM
Applying a VBA code only on a selected text or range Import a specific range (bookmarked section) from all Word docs in a selected folder Greengecko Word VBA 5 06-14-2016 07:54 AM

Other Forums: Access Forums

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