How can I assign a macro to a command button in word 2003?

G

Gelu

Hi Jay & thx.

It is a command button from the Control Toolbox. The link you sent does not
address this issue. Basically a draw a button in the document and try to
assihn to it a macro like:

Sub Enlarge()
'
' Enlarge Macro
' Enlarges a picture placed within the document
'
ActiveDocument.Shapes("Picture 97").Select
Selection.ShapeRange.ScaleWidth 1.12, msoFalse, msoScaleFromTopLeft
Selection.ShapeRange.ScaleHeight 1.12, msoFalse, msoScaleFromTopLeft
End Sub

In Excel this can be done easily by just calling the macro within the click
event.

But foe Word I do not know how to work it out.

Regards,

Gelu
 
J

Jay Freedman

Gelu said:
Hi Jay & thx.

It is a command button from the Control Toolbox. The link you sent
does not address this issue. Basically a draw a button in the
document and try to assihn to it a macro like:

Sub Enlarge()
'
' Enlarge Macro
' Enlarges a picture placed within the document
'
ActiveDocument.Shapes("Picture 97").Select
Selection.ShapeRange.ScaleWidth 1.12, msoFalse,
msoScaleFromTopLeft Selection.ShapeRange.ScaleHeight 1.12,
msoFalse, msoScaleFromTopLeft End Sub

In Excel this can be done easily by just calling the macro within the
click event.

But foe Word I do not know how to work it out.

Regards,

Gelu

It works exactly the same way in Word. Step by step, assuming you're
creating a template:

- Add your Enlarge() code to either the ThisDocument module or an ordinary
module in the template project.
- Insert the command button from the Toolbox.
- (optional) Click the Properties button on the Toolbox and change
properties such as the caption, size, and color of the button.
- Click the View Code button on the Toolbox. This opens the ThisDocument
module with an empty Click procedure for the command button.
- Within the Click procedure, insert a call to Enlarge().
- Close the VBA editor.
- Click the Design Mode button on the Toolbox to exit from design mode.
- Close the Toolbox.
- Save the template.
- Use File > New to base a new document on the template. The command button
should work, calling the Enlarge() procedure.

As a general principle, you should not depend on the names of shapes (such
as "Picture 97") -- Word will change those numbers as the document is
edited. Instead, you should use the object (usually a paragraph) in the
document to which the shape is anchored. Also, you don't have to -- and
shouldn't -- select the shape in order to format it. For example, this bit
of code formats the first (presumably the only) shape anchored in the second
paragraph of the document:

Sub Enlarge()
'
' Enlarge Macro
' Enlarges a picture placed within the document
'
Dim oShp As Shape
On Error GoTo bye ' in case there's no shape there
Set oShp = ActiveDocument.Paragraphs(2).Range.ShapeRange(1)
oShp.ScaleWidth 1.12, msoFalse, msoScaleFromTopLeft
oShp.ScaleHeight 1.12, msoFalse, msoScaleFromTopLeft
Set oShp = Nothing
bye:
End Sub
 

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