How to create and run a macro in MS Word.
The great thing about creating macros in Word is that you don’t need to know how to program. If you can hit the record button and stop, you can do it.
Aren’t Macros Dangerous?
Have you heard that macros are bad because they can contain viruses? While you should be careful when opening Office documents from unknown people as they may contain malicious macros, this is not a problem here. You make your own macro to know that it is not a virus.
How To Record A Macro in Word
- With Word open, click the View tab.
- Click the down arrow below the Macros button.
- Select Record Macro … a new window will open.
- In the Macro name: field, enter a meaningful name for the macro. Spaces are not allowed. Use underscore or dash.
- In the “Save Macro To:” drop-down list, you can choose which documents you want to use for this macro. If you select All Documents (Normal.dotm), the macro will be available to you in every new Word document that you create from now on. If you select one document, it will only apply to that single document. It is usually best to select “All Documents”.
- In the Description: field, specify what the macro does. This is a simple macro with a descriptive name, but as you get more comfortable with the macros, you will tackle more complex tasks, so a description is always a good idea.
- You can select “Assign Macro” to any button. you create with a button or hotkeys that you can select with a keyboard button. There are many keyboard shortcuts already available for Word, so a button works best. Select button. A new window will open titled “Word Options”.
- This is where you assign a button to the macro on the Word ribbon. Select “Customize Ribbon”.
- From the Select Commands From: drop-down list, select Macros. This will show us our macros in the area below it.
- You need somewhere on the ribbon to place the macro. For this example, select Home in the Main Tabs area. Then select “New Group”.
- Select Rename to give it a meaningful name.
- In the Rename window, select one of the icons to represent the group, then enter My Macros in the Display Name field.
- Click OK to apply it. You will see the change in the main tabs area.
- With the new My Macros selected in the Main Tabs area, select the Normal.NewMacros.Insert_Signature macro.
- Select Add to add it to the My Macros group.
- Of course you want to rename it. With the macro selected in the Main Tabs area, select Rename ….
- In the Rename window, select the icon and enter a simple name in the Display Name: field.
- Click OK to commit the change. You will see the change in the main tabs area.
- Click OK below the main tabs area to complete the button for the macro.
- Create any signature block you want. Add a scan of your signature or other images. As you wish. When done, click the down arrow below the Macros button.
- Select Stop Recording. This is it. The signature insert macro has been created.
- Check this out by finding the Insert Signature button you created on the Home tab and select it. Your signature block should magically look exactly the way you created it.
How To Write a Macro
For most macros, recording is the most efficient way to create them. At the end of the day, you will want to do something more difficult. To do this, you must write a macro in Visual Basic for Applications (VBA). We’ve got the best beginner’s guide to VBA, so bookmark it. Let’s create a macro that will show us the average number of words in a sentence to help us determine when we are verbose.
- You need access to the Developer tab, which is hidden by default in Word. In the upper-left corner of Word, select File.
- In the lower left corner, select Options.
- In the window that opens, select “Customize Ribbon”.
- Find “Developer” on the right and check the box next to it.
- Select OK to close the window. The Developer tab will open.
- Select the Developer tab.
- Click the Macros button.
- Enter a meaningful name in the Macro Name field: leave the macro in: as Normal.dotm so that it applies to all Word documents. Always enter a description in the Description: area.
- Select New. The Microsoft Visual Basic for Applications Development Tool opens.
- The code shown in the green boxes below should already be there with a little space in between. Copy and paste the following code here:
Dim s As Range
Dim numWords As Integer
Dim numSentences As Integer
numSentences = 0
numWords = 0
For each s in ActiveDocument.Sentences
numSentences = numSentences + 1
numWords = numWords + s.Words.Count
MsgBox “Average number of words in a sentence” + Str (Int (numWords / numSentences)) + “. Less than 15 is best. ”
- Click the Run button to test the code. If it doesn’t work as expected, edit the code until it runs the way you want it to.
- Click the Save button and close the design window.
- Create a button on the Word ribbon using the same method shown in the How to Record Macro instructions above.
- Find the button you just created and check it out.
My Macro Doesn’t Work When I Save My Word Document
By default, Word saves the file with a .docx extension, which prevents macros from running automatically. If you want the macro to run, you need to change the file type to Word Macro-Enabled Document (* .docm).
This file type exists in part to combat macro security issues. If you see this file extension in a document that you did not expect to receive, be suspicious.
What Else Can I Do With Macros?
You can create macros in Excel, Outlook, and even PowerPoint. Anything you do manually now, you should be able to record or write a macro to do this. Using macros in Word along with the excellent Windows keyboard shortcuts will increase your productivity tenfold.
How to create and run a macro in MS Word