Create a macro in Word

Getting the most out of Eileen's Lounge!
User avatar
Rudi
gamma jay
Posts: 25167
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Create a macro in Word

Post by Rudi »

How to integrate macros into Word
------------------------------------------------
If you are provided with a solution that involves Visual Basic code (VBA or macros) and are unfamiliar with the process to integrate it into Word for use, this short step by step will show you how. First locate the version of Word you are working with, and then apply the steps as illustrated.

In the lounge, VBA code or macros are presented to you in Code Tags. These tags will ensure the code is compatible with the VBA Editor into which you will paste the code. It also conveniently provides a Select All option to easily copy the entire block of code without having to drag to select it. Try it with this example:

Code: Select all

Sub SampleMacro()
    Selection.Font.Color = wdColorRed
    Selection.Font.Size = 48
    Selection.Font.Name = "Bauhaus 93"
    Selection.TypeText Text:="Hello, Word!"
End Sub
Steps to use VBA code
------------------------------
Word 2007 / 2010 / 2013
Please note: Screenshots below are taken from Word 2013. Older versions might have small variances.

1. Use the Select All option to select the VBA code from the post
2. Copy the selected code
3. Open Word and the document where the code must be placed
4. Press ALT+F11 (to open the VBA Editor) - as seen in screenshot below
VBA Editor (W1).jpg
5. Select Insert, Module (unless the post specifies otherwise)
6. Paste the code into the module on the right
7. Click the Save button
8. Close the VBA Editor

To Use/Run the macro in Word
1. Access the VIEW tab of the ribbon
2. Click on the Macros button (and select View Macros) - as seen in screenshot below
Use or Run Macro (W2).jpg
3. Select your macro and click on Run - as seen in screenshot below
Macro Dialog (W3).jpg
--- You can add an optional description for the macro (useful if you have several macros in the same document)
4. If you want to assign shortcut keys to the macro so you can trigger it from the keyboard, follow the points in the screenshot below
Shortcut%20Key%20Assign%20(W4).jpg
--- Access the File ribbon and choose Options (in Word 2007: use the Office Orb and click Word Options)
--- Select 'Customize the Ribbon' (in Word 2007: Customize) and click the button "Customize..." next to Keyboard shortcuts.
--- If you want the macro to be available in all documents, make sure that Normal.dot is selected in the 'Save changes in' box
--- Select Macros in the list of Categories on the left hand side
--- Select your macro in the list of Macros on the right hand side
--- Click in the 'Press new shortcut key' box
--- Press the desired keyboard shortcut
--- Click Assign


Word 2003

1. Use the Select All option to select the VBA code from the post
2. Copy the selected code
3. Open Word and the document where the code must be placed
4. Press ALT+F11 (to open the VBA Editor) - as seen in screenshot below
VBA Editor (W1).jpg
5. Select Insert, Module (unless the post specifies otherwise)
6. Paste the code into the module on the right
7. Click the Save button
8. Close the VBA Editor

To Use/Run the macro in Word
1. Access the Tools menu
2. Click on the Macro, and then Macros command - as seen in screenshot below (or press ALT+F8)
2003%20Word%20Macro.jpg
3. Select your macro and click on Run - as seen in screenshot below
2003%20Word%20Run.JPG
--- You can add an optional description for the macro (useful if you have several macros in the same document)

4. If you want to assign shortcut keys to the macro so you can trigger it from the keyboard, follow the points below
--- In Word, Tools | Customize...
--- Click Keyboard...
--- If you want the macro to be available in all documents, make sure that Normal.dot is selected in the 'Save changes in' box
--- Select Macros in the list of Categories on the left hand side
--- Select your macro in the list of Macros on the right hand side
--- Click in the 'Press new shortcut key' box
--- Press the desired keyboard shortcut
--- Click Assign
--- Click Close
2003%20keyboard%20shortcut.png
You do not have the required permissions to view the files attached to this post.
Last edited by Rudi on 15 Feb 2014, 14:42, edited 7 times in total.
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.