#1
|
|||
|
|||
Limiting a macro to a selection
I cannot determine what command is needed to restrict the following macro, created using the macro recorder, to my selection. I have looked through all my other macros and can't figure out what allows some of them to operate this way. One them works one the whole document if I select nothing, or works on a selection, but why is beyond me, except that it's useful. I have tried to use the recorder to isolate this aspect, to no avail.
Thanks in advance! Code:
Sub ParasStripSelected() ' ' ParasStripSelected Macro ' 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 Selection.MoveLeft Unit:=wdCharacter, Count:=1 End Sub Last edited by Ulodesk; 06-15-2012 at 07:29 AM. Reason: Added a note. |
#2
|
||||
|
||||
It's all to do with the '.Wrap' property, which can Ask, Stop, or Continue.
As for the 'extra' lines, the macro recorder does generate quite verbose code. Your macro could at least be reduced to: Code:
Sub ParasStripSelected() With Selection.Find .ClearFormatting .Replacement.ClearFormatting .Text = "^p" .Replacement.Text = " " .Forward = True .Wrap = wdFindAsk .Format = False .MatchWildcards = False .Execute Replace:=wdReplaceAll End With End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
Macro
Thank you, Paul, but the macro you provided doesn't seem to work -- though I am relcutant to even suggest this. Perhaps I have once again managed to state my problem unclearly. I copied your macro into my VBA and ran it on five selected paragraphs (single lines I had created, each with a hard return), which start several paragraphs down (see attached document). Like my previous, recorded macro, it did not stop with those five lines but continued all the way to the end of the document, merging 20 paragraphs, reporting this, and asking if I wished to check the rest of the document.
In between the time I posted my question and received your reply, I had tried one other thing over the weekend, on a guess, which was to make Wrap false, which work did work. I was going to post this this morning when I saw that you had replied, so I tried yours first. The working macro now reads thus, with a few superfluous lines removed, per your example: Code:
Sub ParasStripSelected() ' ' ParasStripSelected Macro ' ' Selection.Find.ClearFormatting Selection.Find.Replacement.ClearFormatting With Selection.Find .Text = "^p" .Replacement.Text = "" .Forward = True .Wrap = False .Format = False .MatchCase = False .MatchWildcards = False .Execute Replace:=wdReplaceAll End With End Sub |
#4
|
||||
|
||||
Hi Ulodesk,
My intention in modifying your macro was not to change anything about how it worked, but only to show how it could be streamlined. Functionally, it's exactly the same as what you posted. As also mentioned in my post, the 'continuation' behaviour is governed by the '.Wrap' property, which can Ask, Stop, or Continue. If you change '.Wrap = wdFindAsk' to '.Wrap = wdFindStop', then only the selected range will be processed - except that if nothing is selected then everything forward of the insertion point will be processed.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
Thanks for the clarification, Paul. I join the many who find ourselves continually grateful for your dedicated time and expertise on this forum.
|
Tags |
limit macro |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Selection is locked | Thokkle | Word | 1 | 04-01-2012 06:46 PM |
How do you add to a selection list? | bryanarn | Excel | 2 | 03-05-2012 05:04 PM |
Macro to populate a text form field based on dropdown selection | koloa | Word | 0 | 10-20-2011 11:52 AM |
Limiting the times that it runs | hchbiker | PowerPoint | 0 | 02-09-2011 08:36 AM |
Save Selection | cksm4 | Word VBA | 25 | 01-30-2011 11:44 PM |