limiting operation of a macro to selected text

G

gvm

I recorded the following macro to replace line breaks with spaces. However, I
want it to replace line breaks only in the text that was selected before
invoking the macro. The following code replaces all line breaks in the whole
document.

Also, what do I need to do to ensure the macro is available to any and every
Word document that is open on this computer please? TIA .... Greg

Here's the code:
Sub Macro1()
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^p"
.Replacement.Text = " "
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
 
T

Tony Jollans

I suspect that, if you recorded it, the macro is in the NewMacros module in
Normal.dot and, if so, it is a lready available to all documents.

The macro works on the Selection or, if the selectiion is just an insertion
point, it will work from it to the end of the document. When done it should
then ask if you want to continue for the rest of the document (maybe it
doesn't do this if the selection was at the start of the document to begin
with). If you change wdFindAsk to wdFindStop and make sure you have a
selection to begin with, then it will only work on that selection ....

Sub Macro1()
If Selection.Type = wdSelectionIP Then Exit Sub
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^p"
.Replacement.Text = " "
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
 

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