Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / April 2005

Tip: Looking for answers? Try searching our database.

Restrict Find&Repalce to Selected Text

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
TT - 22 Apr 2005 01:58 GMT
I would like to write a macro to do multiple Find/replace operations but I
want the operations confined to a block of text selected by the user. Can
anyone point me to an example or sketch the approach?

Thanks!
Vince - 22 Apr 2005 02:23 GMT
Somthing like this will be needed.

After the user has selected the text:

Dim R1 as range
set R1=selection.range ' Assign R1 to be the range of the selected text

with R1.find ' you will find only in the selected text
   .text="Blah"

   with .replacement
       .text="Blah2"
   end with
   .execute replace:=wdreplaceall
end with

> I would like to write a macro to do multiple Find/replace operations but I
> want the operations confined to a block of text selected by the user. Can
> anyone point me to an example or sketch the approach?
>
> Thanks!
Jay Freedman - 22 Apr 2005 03:32 GMT
>I would like to write a macro to do multiple Find/replace operations but I
>want the operations confined to a block of text selected by the user. Can
>anyone point me to an example or sketch the approach?
>
>Thanks!

The key to this is to use a Range object to do the search in a loop,
and check each iteration of the loop to make sure the range is still
within the selected area:

Sub foo()
   Dim oRg As Range
   
   Set oRg = Selection.Range
   With oRg.Find
       .Format = False
       .Forward = True
       .Wrap = wdFindContinue
       .MatchWildcards = False
       .Text = "dolor"
       .Replacement.Text = "dollar"
       
       Do While .Execute(Replace:=wdReplaceOne) _
           And oRg.InRange(Selection.Range)
           ' InRange is true if oRg is inside Selection.Range
       Loop
   End With
End Sub

--
Regards,
Jay Freedman
Microsoft Word MVP         FAQ: http://word.mvps.org
Greg Maxey - 22 Apr 2005 04:11 GMT
Jay,

My test of the code you posted isn't working.

I typed:

Now is the time for all good men.

Now is the time for all good men.

Then selected the first.

Running

Sub FRinSelectionOnly()
Dim oRg As Range
Set oRg = Selection.Range
With oRg.Find
 .Format = False
 .Forward = True
 .Wrap = wdFindContinue
 .MatchWildcards = False
 .Text = "time"
 .Replacement.Text = "hour"
 Do While .Execute(Replace:=wdReplaceOne) And oRg.InRange(Selection.Range)
 Loop
End With
End Sub

Resulted in "time" being replaced by "hour" in both the selected and
non-selected sentences.

I changed your code a bit to:

Sub FRinSelectionOnly1()
Dim oRg As Range
Set oRg = Selection.Range
With oRg.Find
 .Format = False
 .Forward = True
 .Wrap = wdFindStop
 .MatchWildcards = False
 .Text = "time"
 .Replacement.Text = "hour"
 Do While .Execute(Replace:=wdReplaceAll)
 Loop
End With

Using wdFindStop vice wdFindContinue resulted in only the first (and
selected) sentence being altered.

Signature

Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

>> I would like to write a macro to do multiple Find/replace operations
>> but I want the operations confined to a block of text selected by
[quoted text clipped - 24 lines]
>    End With
> End Sub
Jean-Guy Marcil - 22 Apr 2005 07:06 GMT
Greg Maxey was telling us:
Greg Maxey nous racontait que :

> Jay,
>
[quoted text clipped - 46 lines]
> Using wdFindStop vice wdFindContinue resulted in only the first (and
> selected) sentence being altered.

This works:

Dim oRg As Range

Set oRg = Selection.Range
With oRg.Find
 .Format = False
 .Forward = True
 .Wrap = wdFindContinue
 .MatchWildcards = False
 .Text = "time"
 Do While .Execute And oRg.InRange(Selection.Range)
   oRg.Text = "hour"
 Loop
End With

There something I am not quite getting with the range find/replace.... Jay's
code should work, but it does not, and as you discovered, if you use
wdFindStop, it replaces only the first occurrence... I know I got that to
work once... but it is way too late for me to dig it up right now, tomorrow
maybe?

Signature

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org

Helmut Weber - 22 Apr 2005 07:38 GMT
Hi everybody,
this one works for me:

Sub test4444()
Dim rTmp As Range
Set rTmp = Selection.Range
ResetSearch
With rTmp.Find
  .Text = "width"
  .Replacement.Text = "breite"
  .Execute Replace:=wdReplaceAll
End With
ResetSearch
End Sub

Public Sub ResetSearch()
With Selection.Find
  .ClearFormatting
  .Replacement.ClearFormatting
  .Text = ""
  .Replacement.Text = ""
  .Forward = True
  .Wrap = wdFindContinue
  .Format = False
  .MatchCase = False
  .MatchWholeWord = False
  .MatchWildcards = False
  .MatchSoundsLike = False
  .MatchAllWordForms = False
  ' plus some more if required
  .Execute
End With
End Sub

Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word 2002, Windows 2000
Greg Maxey - 22 Apr 2005 10:56 GMT
Helmut,

Yes it does.  Why does it work this doesn't is very puzzling:

Sub FRinSelectionOnly()
Dim oRg As Range
Set oRg = Selection.Range
With oRg.Find
 .ClearFormatting
 .Replacement.ClearFormatting
 .Text = ""
 .Replacement.Text = ""
 .Forward = True
 .Wrap = wdFindContinue
 .Format = False
 .MatchCase = False
 .MatchWholeWord = False
 .MatchWildcards = False
 .MatchSoundsLike = False
 .MatchAllWordForms = False
 .Execute
 .Text = "time"
 .Replacement.Text = "hour"
 .Execute Replace:=wdReplaceAll
 End With
End Sub

Signature

Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

> Hi everybody,
> this one works for me:
[quoted text clipped - 34 lines]
> "red.sys" & chr(64) & "t-online.de"
> Word 2002, Windows 2000
Helmut Weber - 22 Apr 2005 13:00 GMT
Hi Greg,

>Yes it does.  Why does it work this doesn't is very puzzling:

Yes, indeed.
An additional .Wrap = wdFindStop helps.

>  .Execute
>  .Text = "time"
>  .Replacement.Text = "hour"
  .Wrap = wdFindStop
>  .Execute Replace:=wdReplaceAll

But this is not meant to be an explanation,
maybe a hint to someone who can explain.

Greetings from Bavaria, Germany

Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
Greg - 22 Apr 2005 13:53 GMT
Helmut,

It appears the quirk (that's what I will call it) is in the .Wrap code.

Take your code without the Reset routines and with two .Wrap lines
(both stetted out)

Sub FRinSelectionOnlyC()
Dim oRng As Range
Set oRng = Selection.Range
With oRng.Find
 '.Wrap = wdFindContinue
 '.Wrap = wdFindStop
 .Text = "time"
 .Replacement.Text = "hour"
 .Execute Replace:=wdReplaceAll
End With
End Sub

It works.

Activate .Wrap wdFindContinue it fails
Deactive .Wrap wdFindContinue it works
Activate .Wrap wdFindStop it works
Deactivate .Wrap wdFindStop it works

Remove both and it works.

Sub FRinSelectionOnlyC()
Dim oRng As Range
Set oRng = Selection.Range
With oRng.Find
 .Text = "time"
 .Replacement.Text = "hour"
 .Execute Replace:=wdReplaceAll
End With
End Sub

It appears that you don't need .Wrap wdFindStop for it to work but it
won't work with .Wrap wdFindContinue.
Greg - 22 Apr 2005 14:01 GMT
Helmut,

Consider this:

Sub FRinSelectionOnlyC()
Dim oRng As Range
Set oRng = Selection.Range
With oRng.Find
 '.Wrap = wdFindContinue
 '.Wrap = wdFindStop
 .Text = "time"
 .Replacement.Text = "hour"
 .Execute Replace:=wdReplaceAll
End With
End Sub

Run with both .Wrap lines it stetted out.  It works

Run with .Wrap wdFindContinue it fails
Run with .Wrap wdFindStop it works
Run again with wdFindContinue it fails
Run with both stetted out again it works

The quirk (if that is what we call it) appears to be in tied to the
.Wrap line.
Jay Freedman - 22 Apr 2005 18:50 GMT
Hi Greg,

You're right, my code is trash. I know I had it working last night, but
after I posted it I threw away the file, so I don't know what I did. :(

Anyway, you and Jean-Guy and Helmut have all come up with working versions.
For clarity of understanding -- that is, absence of "magic" -- I prefer
Jean-Guy's version.

Signature

Regards,
Jay Freedman
Microsoft Word MVP          FAQ: http://word.mvps.org

> Jay,
>
[quoted text clipped - 75 lines]
>>    End With
>> End Sub
Greg Maxey - 22 Apr 2005 20:56 GMT
Jay,

"Trash."  That is a little harse :-)

There is a  eastern proverb. "Sometimes even experienced monkeys fall out of
the tree."

JGM, thanks for you suggestion.
Signature

Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

> Hi Greg,
>
[quoted text clipped - 86 lines]
>>>    End With
>>> End Sub
TT - 25 Apr 2005 01:38 GMT
Thanks to everyone who contributed to the solution!
> Jay,
>
[quoted text clipped - 94 lines]
>>>>    End With
>>>> End Sub
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.