PDA

View Full Version : Solved: Assign a Macro to a button



dbhaley
03-04-2009, 03:57 PM
Is there a way to create a button (or hyperlink if that is easier) on my word document that will run the macro of my choice. I have seen lots of tutorials for creating a toolbar button or keyboard shortcut, but that is not what I'm looking for. My co-workers will only be able to understand a little picture that says "click me".

Regards,

Blake

lucas
03-04-2009, 09:16 PM
For 2003 and before. I don't think it will work on 2007

http://slucas.virtualave.net/Wink/CustomMenuItem.htm

dbhaley
03-04-2009, 10:14 PM
Lucas, thank you for your reply. While your link is a great tutorial (I saved it for future use) it is not what I am looking for. I am looking to insert a button on the word document that will run a particular macro when clicked. It does not have to be a button or a single click. Assigning a double click to a shape or picture would work fine as well. This is so users of the document will be able to run the macro without being given any instructions prior to opening the document.

Thanks,
Blake

lucas
03-05-2009, 07:08 AM
If you go to tools-view-and put a check next to visual basic you will see the visual basic toolbar.

Select the button that when you hover over it says "design mode" and then click the button that says " control toolbox.

From the toolbox select command button and you can format it from it's properties by right clicking on it.

while still in design mode, doubleclick the button and you will be taken to the vb editor and you will see the button click procedure and it will be empty. Put your code in that module or call your macro from that buttonclick procedure.

fumei
03-05-2009, 01:17 PM
Here is a demo with two ActiveX commandbuttons. They were inserted into the document using the steps Steve mentions above. Both have "Click Me!" as the caption.

The first one simply displays a messagebox stating "This can be instructions to do whatever you want."

The second one displays the ListCommands dialog. The point being is that the button can execute ANY valid procedure.

NOTE: the ShowCommand dialog will make a new document that lists either:

a) current menu and keyboard commands

b) ALL Word commands.

Please note that selecting all Word commands (and clicking OK) will produce a new 29 page document listing ALL Word commands. You can of course just cancel out, as this is just to demo the use of commandbuttons.

Last note: code (procedures) for ActiveX controls should be in the ThisDocument module, which is the native module for ActiveX control event procedures. You can have procedures in standard modules that will fire from an ActiveX control event, but the control must then be fully qualified. Also, a control event procedure (_Click is the most common) can also fire any procedure in another module by using Call.

fumei
03-05-2009, 01:21 PM
Just for amusment sake...here is the same file again, but I added an image to the second commandbutton. The action taken when clicking it is exactly the same. I just wanted to show that you can make your buttons "pretty".