HOWTO create macro programmatically for command bar buttons.
I want to programmatically create a macro that I then programmatically make
a command bar button for to call via its OnAction. I know how to add the
command bar button dynamically and sets its OnAction, but I can not figure
out how to create a macro (or AddIn) programmatically such that the button
could then call it.
What can one do?
Jezebel - 28 Jul 2006 23:20 GMT
In principle you can create a macro at runtime through the properties and
methods of the application's VBE object. In practice it's damned
difficult --
1. Most security set-ups completely block any programmatic modification of
the code environment (this is a major virus vulnerability).
2. Making changes to the VBA project resets the project -- that is, stops
any running code and clears all variables.
If you already have an add-in, why can't you set up the macro and the
toolbar in advance?
> HOWTO create macro programmatically for command bar buttons.
>
[quoted text clipped - 6 lines]
>
> What can one do?
Jean-Guy Marcil - 29 Jul 2006 03:52 GMT
ATS was telling us:
ATS nous racontait que :
> HOWTO create macro programmatically for command bar buttons.
>
[quoted text clipped - 5 lines]
>
> What can one do?
I am curious here..
Why have a macro that creates a macro?
The first macro has to be stored somewhere, no?

Signature
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org
ATS - 31 Jul 2006 14:44 GMT
Thanks for the reply,
I have seen the VBE object, but I'm not sure how I can use it. As for the
question, "Why have a macro that creates a macro?", I do not have a macro,
nor do I have an AddIn, there are more ways to get to the Application and
Document objects than just macro's and AddIns. It appears that I am going to
have to dynamically create an AddIn just to make this work, and that would be
way too much work, but I do not see a way around it, unless the VBE can help.
At any rate, any sample of a VBE usage that shows how to create a macro that
in turn a button's OnAction can then call?
Jonathan West - 31 Jul 2006 15:15 GMT
> HOWTO create macro programmatically for command bar buttons.
>
[quoted text clipped - 6 lines]
>
> What can one do?
The macro you create would have to be stored somewhere - probably either in
normal.dot or in an add-in.
There are very good reasons why you should not attempt to touch normal.dot -
no least because you have no means of knowing what somebody else has already
done to it. That means you are best using an add-in.
If you have a single macro you want to use (or a limited set of macros) then
you are best creating the macros in the add-in, creating the matching
toolbar buttons, and then loading or unloading the add-in as required. If
necessary, you can refine things further by changing the Visible property of
individual buttons to show or hide them.
Using the VBE object hierarchy is an iffy prospect - it requires that the
user's PC is set up to trust access to the VB project object, and that is
not the default setting in the latest version of Word.
Take a step back from the immediate question, and describe your overall
project a bit. If we understand what you are trying to achieve, we may be
able to suggest an entirely different approach that will meet your
requirements much better.

Signature
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org