Hi Blake,
you are talking about an activeX-control.
For creating code programmatically you need a reference to
microsoft visual basic for application extensibility 5.3 (or your version).
Which is quite difficult to handle, IMHO.
However, hmm, though I don't know about a simple way
of assigning code to an activeX-control,
it seems possible to assign the control to the code (!)
just by changing its name, like this:
Private Sub Cmd1_Click()
MsgBox Me.Cmd1.Name
Cmd1.Name = "Cmd2"
End Sub
Private Sub Cmd2_Click()
MsgBox Me.Cmd2.Name
Me.Cmd2.Name = "Cmd1"
End Sub
There is a commandbutton named "Cmd1".
After execution of cmd1_click,
the button is named Cmd2.
When clicking again on what is now Cmd2,
the code in Cmd2_click is executed,
which includes renamening it to cmd1.
For each first click cmd1 is executed,
for each second click cmd2 is executed.
Have some fun.
--
Greetings from Bavaria, Germany
Helmut Weber, MVP WordVBA
"red.sys" & chr(64) & "t-online.de"
Word 2002, Windows 2000 (german versions)
Helmut: Thanks for your response; I appreciate that interesting
technique.
Maybe if I described the overall project instead of this particular
method, a suitable work-around would be more evident.
I'm developing a Word-based exam-creating script (it pulls a list of
formatted physics problems from a pool and creates a quiz in the form
of an interactive Word document). Each question will need
pushbuttons, etc. to support it (a "Submit response" button, etc.),
and the commands for some buttons will be problem-specific (thus I
cannot create a general "Submit response" button with code that will
work for every question under the current design). The central
problem is thus how to produce problem-specific controls from a macro.
An obvious option is a redesign so that general control templates can
be used for each problem. However, automatically-coded controls would
still be of interest to me for future applications I have in mind and
would be a useful degree of freedom in this application. I'm
approaching VBA controls from a MATLAB programming background, where
the callback function for a control (being a property of that control)
could be modified by other scripts.
Thanks for any help you can provide.
--Blake