Results 1 to 10 of 10
  1. #1
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    run macro only current document (Word 97+)

    I can't find any discussion of this in the VBA or Word forums, so here goes:

    Is it possible to restrict the Tools, Macro, macros to a specific source document under program control?

    I have a macro stored in a document. I want to run the macro. I choose Tools, Macro, Macros, and am presented with a gargantuan list of all macros available to me.
    In the dialogue box I select "Macros in <my document>" and am rewarded with an abbreviated list.
    The generated code, should I record this is<pre>Application.Run MacroName:="DateC"</pre>

    The Help screens offer no indication of a chance to restrict the dialogue to only those macros in the Active Document.
    To be sure, I can quote a file, module and macro and run that way, but only when the user has already selected the macro from the dialogue box.
    Searching Google groups and mskb for "Application.Run dialog" doesn't reveal much, either.

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: run macro only current document (Word 97+)

    I don't know if there is an "official" way to do so, but you could use the infamous SendKeys instruction:

    SendKeys "%a" & ActiveDocument.Name & "~"
    Application.Dialogs(wdDialogToolsMacro).Show

    SendKeys sends Alt+A to activate the "Macros in" dropdown, provides the name of the active document (you can replace it with the name of a specific template if you wish), and finally an Enter to confirm the selection in the dropdown list.

  3. #3
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: run macro only current document (Word 97+)

    In WordBasic, the selection in the drop-down control was specified using the Show argument. However, in VBA .Show is a method of the Dialog object, so it is not possible to use .Show to assign a property. I'm not aware of any replacement (the help file still list Show as the parameter).

  4. #4
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: run macro only current document (Word 97+)

    Thanks, Hans. sendkeys is one thing that has never seemed to work for me, from Win97/Office97 onwards.
    It's a good thought, but I've experimented so much with Sendkeys and failed that I don't consider it nowadays.
    FWIW I'm now up to WinXp professional, and it still doesn't work. Different computer, different Os, ....

  5. #5
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: run macro only current document (Word 97+)

    <hr>It would have mad a nice toolbar button to run a macro called "RunAMacroFromThisDocument".
    I suppose I could make my own GUI form .......<hr>
    It's easier to populate a pop-up menu on that toolbar than to create a dialog. Especially if the only option is to run the macro, rather than to do any other of the other things the Macros dialog enables.

  6. #6
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: run macro only current document (Word 97+)

    >I'm not aware of any replacement
    Jefferson thanks for the reply. This confirms what I'd found to date - that there seems to be no mechanism for assisting the end-user to narrow the serach. It would have made a nice toolbar button to run a macro called "RunAMacroFromThisDocument".
    I suppose I could make my own GUI form .......

  7. #7
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: run macro only current document (Word 97+)

    > populate a pop-up menu on that toolbar than to create a dialog
    True. But, ....
    (1) given a vast number of macros a dialog would let me scroll and
    (2) there's the busienss of the end-user feeling comfortable with a dialog.
    If I did build it, it would be with a view to making the user think that I'd tweaked the over-burdened dialog box.

  8. #8
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: run macro only current document (Word 97+)

    Oh, a "vast number of macros." I thought the point was to simplify to a short list. Never mind.

  9. #9
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: run macro only current document (Word 97+)

    When you're my age you are allowed to get confused .....
    One application template ("Under") has about 600 macros.
    Another template, ("Indxr"), has 30 macros.
    I hate scrolling through a mixture 0f 630 macros trying to find one out of the 30 I need from Indxr.
    I had thought that if I could narrow the set of macros to the "Indxr", I'd have only 30 to scroll through.
    Rather than force the user to select the source I'd provide a toolbar button that read "Populate the Macros dialog with macros from the Active Document, whichever it happens to be right now"

    My frustration lies in discovering one of those rare occasions where the end-user can do something (select the source of macros) that I can't do in VBA. Grrrrrr!

  10. #10
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: run macro only current document (Word 97+)

    As HansV suggested, you can select the list using SendKeys, tho SendMessage would be more reliable.

    If you need further granularity, you can build your own dialog.

    Also, having so many macrros in one template is likely cumbersome.

Posting Permissions

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