PDA

View Full Version : Activating macros



robinmk
06-23-2010, 04:40 AM
Hi,
I'm pretty much a novice at VBA for WORD so still trying to get used to the commands but use it all the time in Excel and generally have relevant macros activated by 'clicking' on a box.

Is it possible to do this in WORD or on some text like 'Click Here' or is the only way to go into Macros, Select Macro name, Run?

Many Thanks

Tinbendr
06-23-2010, 08:10 AM
Which version?

On 2007, right click on menu bar. Select Customize Quick Access Toolbar. In the top dropdown, select Macros. Click right arrow to add to Quick access menu.

avtarxing
06-23-2010, 08:15 AM
Below is a macro which would create a button on your Word Document and links another macro to its click event

Presently it runs a simple messagebox macro. Change it as per your requirements.


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




Incase you need to put a button adjacent to menu buttons, follow these steps:

1. From the Tools menu, choose Customize.
2. Click the Commands tab and then scroll down in the Categories box and select Macros . You’ll see the names of any macros you've created in the Commands box on the right-hand side.
3. Click, hold, and drag your macro onto any toolbar and release. A button with the name of the macro will appear on the toolbar.
4. To associate an icon with this button, right-click it, choose Change Button Image, and click on any icon
5. Next, right-click the button and choose Default Style. Your button will now appear as an icon only

fumei
06-24-2010, 12:17 PM
Good heavens.

You can execute procedures (macros) by a number of ways.

1. Alt-F8, to get the Macros dialog (all macros)

2. Tools > Macros to get the macros dialog (all macros)

3. assigning a keyboard shortcut to a specific macro e.g. Alt-Z to the Sub Whatever() procedure. This can be assigned at creation time (recording), or after the fact - Tools > Customixe > Commands > select a macro and click Keyboard.

4. putting text, text/image, or just image on a toolbar and assigning a procedure (macro) to it.

5. putting a menu item (including a whole new Menu if yopu need to) that is assigned to a procedure (macro).

6. creating a MACROBUTTON in the document, and assigning a procedure

7. inserting an ActiveX commandbutton, and assiging a procedure( macro)

8. inserting ActiveX optionbuttons, where, for example, if OptionButton_X is clicked (True) then macro_A is fired; if OptionButton_Y is clicked (True) then macro_B is fired

9. inserting an ActiveX combobos (dropdown). Depending on user choice specific macros can be fired - or multiple macros

10. inserting a formfield and using its OnEntry or OnExit attributes to execute a macro...on entering, or on exiting the formfield

11. Document automation events. Executing a procedure (macro) when: document is opened, document is saved, document is closed

12. ANY Word event. You could attach a secondary set of instructions to other common actions. For example:
Sub Bold()
ActiveDocument.Variables("IMadeIt_Bold").Value = _
ActiveDocument.Variables("IMadeIt_Bold") + 1
Selection.Font.Bold = wdToggle
End Sub
This adds extra instructions to using the Bold button. Every time you click the Bold button, a variable value is incremented. then: Sub Document_Close()
MsgBox "In this session, you have used the Bold button " & _
ActiveDocument.Variables("IMadeIt_Bold").Value & _
" times."
ActiveDocument.Variables("IMadeIt_Bold").Value = 0
End Sub
When the document is closed, a message displays stating the number of times you used the Bold button. Obviously this particular example is rather silly, but it demonstrates that you can execute macros (procedures) at a wide variety of times and situations.