Macro: Execute for Selected Text Only

G

Guest

I am seeking to execute the following Microsoft Word 2003 find/replace macro
(Windows XP Pro SP2) – but only for a block of text that has been selected.
At present, the macro executes upon the entire document. How can I constrain
its execution to only selected text?

Thank you for your assistance.

Sub Macro()
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^13 {1,}"
.Replacement.Text = "^13"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
 
E

Ed

Try this: Just after Sub Macro(), add the following lines:
Dim myRange as Range
Set myRange = Selection.Range

Then, is every line of your macro, replace "Selection" with "myRange"
(without any quote marks).

Give that a shot and see if it works for you.

Ed
 

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