MankitaP
MankitaP

Reputation: 67

Word Template : Executing VBA macro on a graphical button click

I've created a VBA Macro for word template and I want to associate it to a button(Graphics button, Not keyboard buttons).There are basically two ways to create a macro :

1) Using macro recorder

2) Using VBA for application to write macro from scratch

But I found several issues with these ways :

I tried First one, What I want is to execute a macro on a button onclick event. but in that I am not getting how to assign that macro to that button.

For second option I tried following procedure :

Goto VB by pressing Alt + F11. On the Tools menu, click References. Select the reference for Microsoft Visual Basic for Applications Extensibility. Insert a new module, and then add the following code example.

 Sub Test()

'Add a command button to a new document
Dim doc As Word.Document
Dim shp As Word.InlineShape
Set doc = Documents.Add

Set shp = doc.Content.InlineShapes.AddOLEControl(ClassType:="Forms.CommandButton.1")
shp.OLEFormat.Object.Caption = "Click Here"

'Add a procedure for the click event of the inlineshape
'**Note: The click event resides in the This Document module
Dim sCode As String
sCode = "Private Sub " & shp.OLEFormat.Object.Name & "_Click()" & vbCrLf & _
        "   MsgBox ""You Clicked the CommandButton""" & vbCrLf & _
        "End Sub"
doc.VBProject.VBComponents("ThisDocument").CodeModule.AddFromString sCode

End Sub

This is the code for macro creation through Vb.But here error is coming in secondlast line of code.

Can anyone suggest me how to get an executable macro on a button click? Am I doing any mistakes in above code?

Upvotes: 2

Views: 23043

Answers (2)

Harif87
Harif87

Reputation: 126

In regards to your issue with the command button showing the VB code when clicking instead of actually running - make sure you are not in Design Mode. Look under the Developer tab on your ribbon and make sure Design Mode is not highlighted.

Upvotes: 1

n00b
n00b

Reputation: 1852

For office 2010 that I have:

1.Macros can be assigned to buttons that will show up in top-left corner of your window (Quick Access Toolbar) When you create a macro, it asks if you want to assign it to keyboard or Button where you can select button

2.You can add a command button or any other control that will run a code when you click it. To do so first you have to add 'Command Button' to your ribbon so that you can create button in your page. To add it you can click on Office button, go to Options, then go to Customize Ribbon and then select All commands from Choose commands from drop down list. find the Command Button and add it somewhere to you ribbon. You may have to create a custom tab in your ribbon. When you have the button in your ribbon, then you can add button to your page and after adding the ribbon double-click on it, and you can write code in button's event handler. You can copy & paste your Macro code there or call the macro using call command How to add a button and call a Macro

Upvotes: 4

Related Questions