Limit find and replace

M

Mark Rowland

How do I limit a find and replace run from a macro to
finding and replacing only text on the current page or
paragraph and not on all pages of a document?
Thanks
 
J

Jonathan West

Hi Mark

Mark Rowland said:
How do I limit a find and replace run from a macro to
finding and replacing only text on the current page or
paragraph and not on all pages of a document?
Thanks

You apply the Find object to the Range you want to search, and you set the
Wrap property of the Find object to wdFindStop.

Something like this

With Selection.Paragraphs(1).Range.Find
.Text = "find this"
.Replacement.Text = "replace with this"
.Wrap = wdFindStop
.Execute Replace:=wdReplaceAll
End With
 
J

Jay Freedman

Hi, Mark,

There are two things to pay attention to:

- The area you want to search has to be selected (if you're using
Selection.Find) or made the extent of a Range object (if you're using
myRange.Find). To select the page that currently contains the cursor,
you can use the built-in bookmark named \Page, like this:
ActiveDocument.Bookmarks("\Page").Select
The current paragraph is just Selection.Paragraphs(1), regardless of
where in the document the cursor is.

- In the .Find object, set .Wrap = wdFindStop.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top