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 / November 2005

Tip: Looking for answers? Try searching our database.

limiting operation of a macro to selected text

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
gvm - 23 Nov 2005 02:45 GMT
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
Tony Jollans - 23 Nov 2005 07:59 GMT
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

--
Enjoy,
Tony

> 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
[quoted text clipped - 22 lines]
>     Selection.Find.Execute Replace:=wdReplaceAll
> End Sub
gvm - 23 Nov 2005 10:26 GMT
Thanks Tony, that works fine,
thanks so much ... Greg
Tony Jollans - 23 Nov 2005 10:45 GMT
My pleasure

--
Enjoy,
Tony

> Thanks Tony, that works fine,
> thanks so much ... Greg
 
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.