need a macro that creates coded controls

B

Blake

I want to write a VBA macro that will create a control in MS Word
(2003) AND assign code to it (such as perform a MoveRight 3 characters
when a pushbutton is activated, just as an example). I've searched
for this on forums, msdn2, and the web and have not yet found
pertinent material for doing this (sorry if I missed it). I'd
appreciate any help/suggestions. Thank you kindly.
 
H

Helmut Weber

Hi Blake,
I want to write a VBA macro that will create a control

there are different kind of controls:
userform-controls, commandbar-controls
activeX-controls

Which one do you want?

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
B

Blake

Hi Blake,


there are different kind of controls:
userform-controls, commandbar-controls
activeX-controls

Which one do you want?

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"

I think what I'm after is a commandbar control; they're the
pushbuttons, checkboxes, etc. available from "Control Toolbox"
toolbar.

As example, a macro creating such a pushbutton would be:

ActiveDocument.ToggleFormsDesign
Selection.InlineShapes.AddOLEControl
ClassType:="Forms.CommandButton.1"

The central problem is assigning code to this pushbutton from within a
macro. It seems similar to having a macro create another macro
(something I have not seen done before). The code could be associated
with either a particular document or normal.dot. Thanks for any help
you can provide.
 
H

Helmut Weber

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.
 
B

Blake

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
 
B

Blake

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- Hide quoted text -

- Show quoted text -

FYI, I followed up the extensibility option, it sounds like it will do
what I want. Helmut: thanks for making me aware of it.

A good description for using extensibility (for Excel) is at
http://www.cpearson.com/excel/vbe.aspx.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top