Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 06-15-2012, 07:27 AM
Ulodesk Ulodesk is offline Limiting a macro to a selection Windows 7 64bit Limiting a macro to a selection Office 2010 64bit
Word 2013 Expert Cert
Limiting a macro to a selection
 
Join Date: Sep 2009
Location: Virginia
Posts: 866
Ulodesk is on a distinguished road
Default 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
I suspect that al those = False lines could be deleted, but I'll wait to have a working macro to try that out.

Last edited by Ulodesk; 06-15-2012 at 07:29 AM. Reason: Added a note.
Reply With Quote
  #2  
Old 06-15-2012, 02:56 PM
macropod's Avatar
macropod macropod is offline Limiting a macro to a selection Windows 7 64bit Limiting a macro to a selection Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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
Further reductions are also possible, but you're not going to gain anything in processing speed and, depending on what the previous Find/Replace used as its parameters, you might need to clear/reset them anyway.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 06-18-2012, 07:53 AM
Ulodesk Ulodesk is offline Limiting a macro to a selection Windows 7 64bit Limiting a macro to a selection Office 2010 64bit
Word 2013 Expert Cert
Limiting a macro to a selection
 
Join Date: Sep 2009
Location: Virginia
Posts: 866
Ulodesk is on a distinguished road
Default 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
Attached Files
File Type: docx Lorem ipsum dolor sit ame1.docx (18.1 KB, 10 views)
Reply With Quote
  #4  
Old 06-18-2012, 02:25 PM
macropod's Avatar
macropod macropod is offline Limiting a macro to a selection Windows 7 64bit Limiting a macro to a selection Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #5  
Old 06-19-2012, 06:09 AM
Ulodesk Ulodesk is offline Limiting a macro to a selection Windows 7 64bit Limiting a macro to a selection Office 2010 64bit
Word 2013 Expert Cert
Limiting a macro to a selection
 
Join Date: Sep 2009
Location: Virginia
Posts: 866
Ulodesk is on a distinguished road
Default

Thanks for the clarification, Paul. I join the many who find ourselves continually grateful for your dedicated time and expertise on this forum.
Reply With Quote
Reply

Tags
limit macro



Similar Threads
Thread Thread Starter Forum Replies Last Post
Limiting a macro to a selection 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
Limiting a macro to a selection Save Selection cksm4 Word VBA 25 01-30-2011 11:44 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 07:02 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft