Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Other Office > Word VBA
|
Word VBA Discuss using VBA to program Word.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Word VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 4th, 2009, 12:01 PM
Registered User
 
Join Date: May 2009
Posts: 5
Thanks: 1
Thanked 0 Times in 0 Posts
Default Search & Replace Text in Selection/Range

Hi All

I would like to search for & replace text sometimes in just a portion of a document. I have tried the following:

Code:
Set rngArea = Selection.Range
    With rngArea.Find
        .Forward = True
        .Wrap = wdFindContinue
        .Text = "This Text"
        .Replacement.Text = "That Data"
        .Execute Replace:=wdReplaceAll
    End With
However this also finds/replaces text when it is outside of the specified range. I have also tried selecting the text in the range first and using the following:

Code:
Sub SR()
    With Selection.Find
        .Text = "This Text"
        .Replacement.Text = "That Data"
        .Forward = True
        .Wrap = wdFindContinue
        .Execute Replace:=wdReplaceAll
    End With
End Sub
I am clearly totally misunderstanding the way this is supposed to work.

Please can you help me?

Archie
 
Old May 5th, 2009, 02:17 AM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 812
Thanks: 1
Thanked 53 Times in 49 Posts
Default

Hi Archie

Change the wrap to wdWrapStop as in

Code:
 

Sub Search_Within_SelectedArea()
Dim rngArea As Range
Set rngArea = Selection.Range
    With rngArea.Find
        .Forward = True
        .Wrap = wdFindStop
        .text = "This Text"
        .Replacement.text = "That Data"
        .Execute Replace:=wdReplaceAll
    End With
End Sub
Cheers
Shasur
__________________
C# Code Snippets (http://www.dotnetdud.blogspot.com)

VBA Tips & Tricks (http://www.vbadud.blogspot.com)
 
Old May 5th, 2009, 02:19 AM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 812
Thanks: 1
Thanked 53 Times in 49 Posts
Default

Hi Archie

You need to change Wrap to wdFindStop

Sub Search_Within_SelectedArea()
Dim rngArea As Range
Set rngArea = Selection.Range
With rngArea.Find
.Forward = True
.Wrap = wdFindStop
.text = "This Text"
.Replacement.text = "That Data"
.Execute Replace:=wdReplaceAll
End With
End Sub

Cheers
Shasur
__________________
C# Code Snippets (http://www.dotnetdud.blogspot.com)

VBA Tips & Tricks (http://www.vbadud.blogspot.com)
 
Old May 5th, 2009, 02:21 AM
Registered User
 
Join Date: May 2009
Posts: 5
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Works a treat. Thank you so much.

Archie





Similar Threads
Thread Thread Starter Forum Replies Last Post
Search and Replace a text in XML file AyatKh XML 17 May 23rd, 2012 09:05 PM
How to get nodes from Selection/Range kaps77 Javascript How-To 1 June 17th, 2007 07:38 AM
Global Search&Replace Text in all fields in sql db buddyz SQL Server 2000 10 September 14th, 2006 08:24 AM
Range selection keithd Excel VBA 7 April 7th, 2005 11:29 AM
RTF Search & Replace PC User Access 0 June 16th, 2004 01:46 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.