Results 1 to 14 of 14
  1. #1
    5 Star Lounger
    Join Date
    Jul 2003
    Location
    USA
    Posts
    728
    Thanks
    7
    Thanked 2 Times in 2 Posts

    Macro to Select text btw Sub & End Sub (Word97/SR2)

    <P ID="edit"><FONT SIZE=-1>Edited by acerfmayer on 29-Jul-01 16:42.</FONT></P>Afternoon -

    Trying to create a macro that will find all text between Sub and End Sub and make the text (including the Sub and End Sub) Times New Roman 8 pts. I tried selecting text with Macro Recorder on, but this is not possible. (I thought every keyboard action could be duplicated when recording a macro...)

    Don

  2. #2
    BAM
    Guest

    Re: Macro to Select text btw Sub & End Sub (Word97/SR2)

    Hi acerfmayer,

    Press <Alt F11> to open the VBA Editor, go to Tools/Options and click the Editor format tab. There you'll find you can set the default font face, size, color, etc for your code.

    Will this solve what you want?
    ~~~~~~~~~~~~~~
    Cheers! <img src=/S/smile.gif border=0 alt=smile width=15 height=15>

  3. #3
    5 Star Lounger
    Join Date
    Jul 2003
    Location
    USA
    Posts
    728
    Thanks
    7
    Thanked 2 Times in 2 Posts

    Re: Macro to Select text btw Sub & End Sub (Word97/SR2)

    Thanks for your suggestion, BUT ---

    I want to do this in a Word document: I have a (long) document of macros (and explanations) that I might want to use in the future and to save space/pages, I put the code in a small font.

    Hope that clarifies my quest. TIA

  4. #4
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Macro to Select text btw Sub & End Sub (Word97/SR2)

    The following is a quick and dirty solution - I'm sure it can be made more efficient, as well as set up to loop through the entire document (but the following is all I had time to work out!) - it works, anyway:

    <pre>Sub ApplyFontToSub()
    With Selection.Find
    .ClearFormatting
    .Text = "Sub"
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = True
    If .Execute = True Then
    With Selection
    .Extend Character:="E"
    .Extend Character:="n"
    .Extend Character:="d"
    .Extend Character:=" "
    .Extend Character:="S"
    .Extend Character:="u"
    .Extend Character:="b"
    With .Font
    .Name = "Times New Roman"
    .Size = 8
    End With
    End With
    End If
    End With
    End Sub
    </pre>


  5. #5
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Macro to Select text btw Sub & End Sub (Word97/SR2)

    This one does the entire document in one go:

    <pre>Sub ApplyFontToSub()
    Selection.HomeKey Unit:=wdStory
    With Selection.Find
    .ClearFormatting
    .Text = "Sub ("
    .Forward = True
    .Wrap = wdFindStop
    .Format = False
    .MatchCase = False
    .MatchWholeWord = True
    Do
    If .Execute = True Then
    With Selection
    .Extend Character:="E"
    .Extend Character:="n"
    .Extend Character:="d"
    .Extend Character:=" "
    .Extend Character:="S"
    .Extend Character:="u"
    .Extend Character:="b"
    With .Font
    .Name = "Times New Roman"
    .Size = 8
    End With
    End With
    End If
    Loop Until .Execute = False
    End With
    End Sub
    </pre>


  6. #6
    5 Star Lounger
    Join Date
    Jul 2003
    Location
    USA
    Posts
    728
    Thanks
    7
    Thanked 2 Times in 2 Posts

    Re: Macro to Select text btw Sub & End Sub (Word97/SR2)

    Gary,

    Thanks so much for both macros. I better understand how to use Extend because of them.

    The macro that works on the entire document is great, but I did want to leave my explanations and notes in my default font and only have the code affected by running the macro.

    The macro with 2nd line

  7. #7
    BAM
    Guest

    Re: Macro to Select text btw Sub & End Sub (Word97/SR2)

    Hi ACM,

    I realize that my previous suggestion wasn't what you are looking for, however it sounds like you are trying to "reinvent the wheel". <img src=/S/smile.gif border=0 alt=smile width=15 height=15>

    In the VBA Editor window and Tools/Options, the Editor Format will do precisely what you are looking for. It will allow you to change the font/size for Comment Text and Normal text.

    So you can have Comment text (those lines preceded with an apostrophe ( ' )) one font/size and the macro text another font/size.

    I understand that you have placed the macros in a document rather than placing them in modules. However perhaps if I describe the system I use you may get some ideas to organize your macros more efficiently. <img src=/S/smile.gif border=0 alt=smile width=15 height=15>

    I create several templates that hold macros for various uses. Within the template, I place the macro names and descriptions in the document and place the macros in various modules so they are categorized.

    Within the document and macro descriptions, I use MacroButton fields for the macro names so I can double-click the macro name to run the macro. For example: {macrobutton TemplateLoad TemplateLoad}

    If I want to edit the macro and view the code, I have a macro stored in Normal.dot that will Edit the selected macro:

    <pre>Sub EditMacro()
    With Dialogs(wdDialogToolsMacro)
    .Name = Selection
    .Edit = True
    . Execute
    End With
    End Sub
    </pre>


    This way, I just double-click a macro name to select it and run the EditMacro macro. This will open the VBA editor directly to the macro.

    I find that these methods allow me to quickly scan a page or so for a specific macro, rather than scan many pages interspersed with descriptions and code, and then run the macro or edit it with a few click of the mouse. <img src=/S/grin.gif border=0 alt=grin width=15 height=15>
    ~~~~~~~~~~~~~
    Cheers!

  8. #8
    5 Star Lounger
    Join Date
    Jul 2003
    Location
    USA
    Posts
    728
    Thanks
    7
    Thanked 2 Times in 2 Posts

    Re: Macro to Select text btw Sub & End Sub (Word97/SR2)

    Hi, BAM -

    Thank YOU for your well written post. And for its gentlemanly tone. I shall certainly study it more fully. I like your EditMacro. I did not realize you could have 2 different font sizes, so thank you for that, too.

    The reason that I like to keep some macros in a Word document rather than 'entering' them in the VBA is for educational purposes.

    Some I might use rarely but I find intriguing and I don't want to clutter the Macros I use day in & day out.

    I just might create several templates, as you suggest.
    Gotta bite that bullet, hmmmmm?

    Thanks, and good evening, ACM

  9. #9
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Macro to Select text btw Sub & End Sub (Word97/SR2)

    Hi ACM,

    My comment attached to the second macro could have been clearer - "do the whole document in one go" intended to convey: "You need run this macro only once in your document. It will search the entire document, and change the font formatting only for text blocks that begin with Sub ( and end with End Sub - everything else gets left unchanged".

    So please give that one a try - I think it does do what you are looking for.

    Regards,
    Gary

  10. #10
    BAM
    Guest

    Re: Macro to Select text btw Sub & End Sub (Word97/SR2)

    Hi ACM,

    You are more than welcome!

    I had to sit down one day and "bite the bullet" as well. I just had too much clutter. Everything was stored in a single global template and was starting to decrease my Word load time. Not to mention that finding what I was looking for was getting to be pretty time consuming! <img src=/S/sigh.gif border=0 alt=sigh width=15 height=15>

    If I may expand further on other methods I use you may get some more ideas here. <img src=/S/grin.gif border=0 alt=grin width=15 height=15>

    I have a couple templates that contain my most commonly used macros. I place a shortcuts to these templates in my WordStartup folder so they automatically load as global templates when Word starts and all macros are available to any document.

    The other templates; references for questions in the Lounge/various newsgroups, specific tasks, Macros?.dot Support?.dot, ect. I manually load as global templates under Tools/Templates and Add-Ins.

    In Tools/Templates and Add-Ins, all you have to do is click the Add command to manually load them. When Word restarts they will still be in the list, just unloaded. When you need them all you have to do is go to Tools/Templates and Add-In and tick the appropriate template. Or you could create a macro to do this for you.

    I have a UserForm that lists each template in an option group and a brief description of what they contain. Then all I have to do is select the appropriate option and it is added in for me.

    I also have shortcuts to all of them, including those in my Startup folder, under the Work menu. That way I can open them at any time to make modifications.

    If you aren't familiar with the Work menu:
    - Right-click any toolbar and select Customize
    - On the Commands tab scroll down and locate the Built-in menus category
    - Drag/Drop the Work menu from the Commands to a location of your choice

    Although there is an "Add to Work Menu" command that you can use to add a shortcut for the active document, there isn't a "Remove from Work Menu" command. However you can use a keyboard shortcut that will remove menu items for this.

    Should you want to remove a shortcut:
    - Press <Ctrl Alt Hyphen> (mouse pointer will change to a minus sign)
    - Open the Work menu and click the item you wish to remove

    Note that this command will remove any menu item - not just those found under the Work menu.

    To make the removal command easy to remember I modified the Name of the WorkAdd To Work Menu command to include the shortcut so I wouldn't forget. If interested in doing the name, open the Tools/Customize dialog box, right-click the command under the Work menu and add the text to the Name.

    Good Luck! And believe me it was worth the little extra work to get myself organized!
    ~~~~~~~~~~~~
    Cheers! <img src=/S/smile.gif border=0 alt=smile width=15 height=15>

  11. #11
    5 Star Lounger
    Join Date
    Jul 2003
    Location
    USA
    Posts
    728
    Thanks
    7
    Thanked 2 Times in 2 Posts

    Re: Macro to Select text btw Sub & End Sub (Word97/SR2)

    Gary - Apologies!! Maybe I had to wait for the Setting Sun-- when I re-ran both your macros, they both worked as promised. You're right, the 2nd macrot does deliver. And you were clear -- I was dim.

    Many many thanks. It's terrific.

    ACM

  12. #12
    5 Star Lounger
    Join Date
    Jul 2003
    Location
    USA
    Posts
    728
    Thanks
    7
    Thanked 2 Times in 2 Posts

    Re: Macro to Select text btw Sub & End Sub (Word97/SR2)

    BAM

    'If I may expand..." you ARE kididng. Anything/Everything you say is worthwhile. Where do you find the time? Do you zip around the globe so it's never evening for you?

    Many thanks. You and Gary are wonderful.

    ACM

  13. #13
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts

    Re: Macro to Select text btw Sub & End Sub (Word97/SR2)

    An alternative method uses the Wildcards and works like this. This is a single search and replace and wouldn't even need VBA if only you could remember the wildcard switches that take me 10 minutes to work out when I need them.
    <pre>Sub temp1()
    Selection.HomeKey Unit:=wdStory
    With Selection.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Replacement.Font.Name = "Courier New"
    .Replacement.Font.Size = 8
    .text = "(Sub*End Sub)"
    .Replacement.text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Format = True
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = True
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    .Execute Replace:=wdReplaceAll
    End With
    End Sub</pre>

    Andrew Lockton, Chrysalis Design, Melbourne Australia

  14. #14
    BAM
    Guest

    Re: Macro to Select text btw Sub & End Sub (Word97/SR2)

    Hi ACM,

    Thank you for your kind compliments. <img src=/S/blush.gif border=0 alt=blush width=15 height=15>

    No I don't zap around the globe - wish I could! <img src=/S/smile.gif border=0 alt=smile width=15 height=15>

    You know what they say about addictions

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •