- Kutools for Excel
- Kutools for Outlook
- Kutools for Word
- EULA End User License Agreement
- Setup Made Simple Easily Install or Uninstall
- Get 4 Sotware Package
- 60-Day Refund 60-Day Risk-Free Guarantee
- Tips & Tricks for Excel (3000+)
- Tips & Tricks for Outlook (1200+)
- Tips & Tricks for Word (300+)
- Excel Functions (498)
- Excel Formulas (350)
- Excel Charts
- Outlook Tutorials
- About Us Our Team
Feature Tutorials
- Search Search more
- Retrieve License Lost License?
- Report a Bug Bug Report
- Forum Post in Forum
How to apply a button to run a macro code in Word document?
Sometimes, you may want to apply a button to run a specific macro code in a Word document, but, how could you solve this task in Word file?
Apply a button to run a specific macro code in Word document
The following steps can help you to deal with this job in Word document, please do as this:
1 . Click where you want to use a button, and then press Ctrl +F9 key to insert a pair of braces, and then type the below text between the braces, see screenshot:
MACROBUTTON MacroName Double-Click To Run Macro
Note : MacroName is the code name in your Word file that you want to run when clicking the button, please change it to your need.
2 . Then, right click the field code, and select Toggle Field Codes from the context menu, see screenshot:
3 . And then, you can format the field text with border and shading for making it as a button. Please select the field text, and then choose Borders and Shading from the Borders drop down list under the Home tab, see screenshot:
4 . In the Borders and Shading dialog box, specify the border style and shading color as you need. See screenshot:
5 . Then, click OK , and the field text has been formatted as a button, now when double clicking this button, your specific macro code will be triggered at once, see screenshot:
Best Office Productivity Tools
Kutools for word - elevate your word experience with over 100 remarkable features.
Dive into the highlighted features below or click here to explore the full power of Kutools for Word .
đ Document Mastery : Split Pages / Merge Documents / Export Selection in Various Formats (PDF/TXT/DOC/XLSX) / Batch Convert to PDF / Export Pages as Images / Print Multiple Files at once ...
â Contents Editing : Batch Find and Replace across Multiple Files / Resize All Pictures / Transpose Table Rows and Columns / Convert Table to Text ...
đ§č Effortless Clean : Sweap away Extra Spaces / Section Breaks / All Headers / Text Boxes / Hyperlinks / For more removing tools, head to our Remove Group
â Creative Inserts : Insert Thousand Separators / Check Boxes / Radio Buttons / QR Code / Barcode / Diagonal Line Table / Equation Caption / Image Caption / Table Caption / Multiple Pictures / Discover more in our Insert Group
đ Precision Selections : Pinpoint specific pages / tables / shapes / heading paragraphs / Navigate with ease using our Select Group
â Star Enhancements : Navigate swiftly to any location / auto-insert repetitive text / seamlessly toggle between document windows / 11 Conversion Tools ...
Transform your Word tasks with Kutools. đ Download with 30-day trial Now đ.
Create or run a macro
In Word, you can automate frequently used tasks by creating and running macros. A macro is a series of commands and instructions that you group together as a single command to accomplish a task automatically.
To save time on tasks you do often, bundle the steps into a macro. First, you record the macro. Then you can run the macro by clicking a button on the Quick Access Toolbar or pressing a combination of keys. It depends on how you set it up.
Record a macro with a button
Click View > Macros > Record Macro .
Type a name for the macro.
To use this macro in any new documents you make, be sure the Store macro in box says All Documents (Normal.dotm) .
To run your macro when you click a button, click Button .
Click the new macro (itâs named something like Normal.NewMacros.<your macro name>), and click Add .
Click Modify .
Choose a button image, type the name you want, and click OK twice.
Now itâs time to record the steps. Click the commands or press the keys for each step in the task. Word records your clicks and keystrokes.
Note:Â Use the keyboard to select text while youâre recording your macro. Macros donât record selections made with a mouse.
To stop recording, click View > Macros > Stop Recording .
The button for your macro appears on the Quick Access Toolbar.
To run the macro, click the button.
Record a macro with a keyboard shortcut
To run your macro when you press a keyboard shortcut, click Keyboard .
Type a combination of keys in the Press new shortcut key box.
Check to see whether that combinationâs already assigned to something else. If it's already assigned, try a different combination.
To use this keyboard shortcut in any new documents you make, be sure the Save changes in box says Normal.dotm .
Click Assign .
To run the macro, press the keyboard shortcut keys.
Run a macro
To run a macro, click the button on the Quick Access Toolbar, press the keyboard shortcut, or you can run the macro from the Macros list.
Click View > Macros > View Macros .
In the list under Macro name , click the macro you want to run.
Click Run .
Make a macro available in all documents
To make a macro from one document available in all new documents, add it to the Normal.dotm template.
Open the document that contains the macro.
Click Organizer .
Click the macro you want to add to the Normal.dotm template, and click Copy .
Add a macro button to the ribbon
Click File > Options > Customize Ribbon .
Under Choose commands from , click Macros .
Click the macro you want.
Under Customize the ribbon , click the tab and custom group where you want to add the macro.
If you don't have a custom group, click New Group . Then click Rename and type a name for your custom group.
Click Add .
Click Rename to choose an image for the macro and type the name you want.
Click OK twice.
Write a macro from scratch in Visual Basic
On the Developer tab, in the Code group, click Macros .
In the Macro name box, type a name for the macro.
Note:Â If you give a new macro the same name as a built-in macro in Word, the new macro actions will replace the built-in macro. To view a list of built-in macros, click Word Commands in the Macros in list.
In the Macros in list, click the template or document in which you want to store the macro.
To make your macro available in all documents, be sure to click Normal.dotm .
Click Create to open the Visual Basic Editor.
After you open the Visual Basic Editor, you may want more information about working with Visual Basic for Applications. For more information, click Microsoft Visual Basic Help on the Help menu or press F1.
You can record a sequence of actions, or you can write a macro from scratch by entering Visual Basic for Applications code in the Visual Basic Editor.
Note:Â To work with macros in Office Word 2007, you need to show the Developer tab.
Show the Developer tab
Click Popular .
Under Top options for working with Word , select the Show Developer tab in the Ribbon check box.
Note:Â The Ribbon is a component of the Microsoft Office Fluent user interface.
Record a macro
On the Developer tab, in the Code group, click Record Macro .
Note:Â If you give a new macro the same name as a built-in macro in Office Word 2007, the new macro actions will replace the built-in macro. To view a list of built-in macros, on the Developer tab, in the Code group, click Macros . In the Macros in list, click Word Commands .
In the Store macro in box, click the template or document in which you want to store the macro.
Important:Â To make your macro available in all documents, be sure to click Normal.dotm .
In the Description box, type a description of the macro.
Do one of the following:
Begin recording     To begin recording the macro without assigning it to a button on the Quick Access Toolbar or to a shortcut key, click OK .
Create a button     To assign the macro to a button on the Quick Access Toolbar, do the following:
Click Button .
Under Customize Quick Access Toolbar , select the document (or all documents) for which you want to add the macro to the Quick Access Toolbar.
Under Choose commands from dialog box, click the macro that you are recording, and then click Add .
To customize the button, click Modify .
Under Symbol , click the symbol that you want to use for your button.
In the Display name box, type the macro name that you want to display.
Click OK twice to begin recording the macro.
The symbol that you choose is displayed in the Quick Access Toolbar. The name that you type is displayed when you point to the symbol.
Assign a keyboard shortcut     To assign the macro to a keyboard shortcut, do the following:
Click Keyboard .
In the Commands box, click the macro that you are recording.
In the Press new shortcut key box, type the key sequence that you want, and then click Assign .
Click Close to begin recording the macro.
Perform the actions that you want to include in the macro.
Note:Â When you record a macro, you can use the mouse to click commands and options, but not to select text. You must use the keyboard to select text. For more information about selecting text by using the keyboard, see Select text .
To stop recording your actions, click Stop Recording in the Code group.
Change the keyboard shortcut for a macro
Click Customize .
Next to Keyboard shortcuts , click Customize .
In the Categories list, click Macros .
In the Macros list, click the macro that you want to change.
In the Press new shortcut key box, type the key combination that you want to choose.
Check the Current keys box to make sure that you aren't assigning a key combination that you already use to perform a different task.
In the Save changes in list, click the option that matches where you want to run your macro.
Click Close .
In the list under Macro name , click the macro that you want to run.
Note:Â If you give a new macro the same name as a built-in macro in Office Word 2007, the new macro actions will replace the built-in macro. To view a list of built-in macros, click Word Commands in the Macros in list.
To make quick work of tasks that you do often, in the desktop version of Word you can bundle the steps into macros that run with a single click. In Word for the web, you have to step through such tasks manually.
If you have Word, first click Open in Word to open your document in Word.
Then follow the instructions for the desktop version of Word .
When you run the macro in Word and save the document, youâll see the results of the macro when you reopen your doc in Word for the web.
Need more help?
Want more options.
Explore subscription benefits, browse training courses, learn how to secure your device, and more.
Microsoft 365 subscription benefits
Microsoft 365 training
Microsoft security
Accessibility center
Communities help you ask and answer questions, give feedback, and hear from experts with rich knowledge.
Ask the Microsoft Community
Microsoft Tech Community
Windows Insiders
Microsoft 365 Insiders
Was this information helpful?
Thank you for your feedback.
- > Word Tips
- > Quick Parts
- > Fields
Assigning a Macro to a Button in Your Text
Written by Allen Wyatt (last updated August 12, 2017) This tip applies to Word 2007, 2010, 2013, and 2016
You may already know that you can only assign a macro to a button (a tool) on the Quick Access Toolbar . Word also allows you to add buttons within the text of you document. These buttons have a macro or a Word command assigned to them, and you can control what happens when the button is selected in text. This is all instituted through the use of a field code.
It may be a bit of a misnomer to refer to the result of this field code as a "button," because no graphics are involved whatsoever, although you can create your own graphic and embed it into the field. The syntax for the field code is:
MacroName is the name of the macro or command you want to run, and Display is the text you want displayed by the field code. If you use a graphic instead of text, then the graphic is displayed. When a user double-clicks on the displayed text or graphic, then the macro or command defined by MacroName is executed.
To assign a command or macro to a button, and insert that button in your text, follow these steps:
- Figure out the name of the macro or Word command you want to use with the button. To find the correct macro or command names you can right click on a blank area of the ribbon then click on Customize (Word 2007) or Customize Quick Access Toolbar (Word 2010 and later). You can find the names in the dialog box.
- Position the insertion point where you want the button to appear.
- Press Ctrl+F9 to insert a pair of field brackets.
- Between the field brackets, type the fieldname MacroButton followed by a space.
- Type the name of the command or macro you want the button to execute, followed by a space. (This is the name you determined in step 1.)
- Type the button name text you want displayed, or insert a graphic to be used as a button.
- Press F9 to update the field display.
Note: If you would like to know how to use the macros described on this page (or on any other page on the WordTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab .
WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (8658) applies to Microsoft Word 2007, 2010, 2013, and 2016. You can find a version of this tip for the older menu interface of Word here: Assigning a Macro to a Button in Your Text .
Allen Wyatt
With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates , a computer and publishing services company. Learn more about Allen ...
MORE FROM ALLEN
Locking the Print Area
Excel allows you to specify an area of your worksheet that should be printed. Here's how to "lock" that area so it cannot ...
Understanding the Notification Area
The Notification Area is located at the right side of the taskbar. It represents an active area of information for ...
Keep with Previous
Word allows you to format a paragraph so that it is on the same page as whatever paragraph follows it. You may want, ...
Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!
More WordTips (ribbon)
Updating a Field in a Text Box
If you put a field into a text box, you might be surprised to find that it doesn't update when you try to update all your ...
Finding Fields
Fields allow you to add simple dynamic content to your document. Here's how you can find the fields when you need to know ...
Inserting the Document Creation Date
One of the pieces of information tracked by Word is when a document was first created. Here's how you can access that ...
FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
View most recent newsletter.
(Your e-mail address is not shared with anyone, ever.)
If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. Youâll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.
(gif, jpeg or png only, 5MB maximum file size)
Notify me about new comments ONLY FOR THIS TIP
Notify me about new comments ANYWHERE ON THIS SITE
Hide my email address
What is seven more than 6?
2019-08-05 08:27:44
Nhat Minh Nguyen
Hi Mr. Wyatt, Thank you for sharing this wonderful trick. I have a question relating to this. I use this method to assign the 'Back button' macro to a button in Word. It worked perfectly. However, when I convert the document into the PDF file, I could not perform the same like in the word (other hyperlinks work normally). My question is, is there any way I can still use this trick when I onvert the word file into PDF?
2019-07-18 12:10:18
Hi Thank you very much for your useful tips. I think we should insert the field in a shape to assign a macro to that shape.
2017-12-09 19:12:24
Graham Skan
I can't get this to work with a graphic in Word 2007. It's OK with text to click on.
2017-08-12 04:55:42
Does this only work for certain commands? I tried to do an Open in the text and nothing happens. I positioned my insertion point where I wanted the field to be, pressed CTRL+F9 and between the braces typed (without quotes) "MacroButton Open OpenIt". Then I pressed F9 and it collapsed to where I just saw the word "OpenIt". It's definitely a field, but when I click on it nothing happens. What's up? Thanks.
Got a version of Word that uses the ribbon interface (Word 2007 or later)? This site is for you! If you use an earlier version of Word, visit our WordTips site focusing on the menu interface .
Visit the WordTips channel on YouTube
View the most recent newsletter.
Links and Sharing
- WordTips FAQ
- Questions or Comments
- Great Branding Ideas
- Free Calendars
OUR COMPANY
- Sharon Parq Associates, Inc.
- About Tips.Net
- Advertise with Us
- Our Privacy Policy
- Author Index
OUR PRODUCTS
- Helpful E-books
- Newsletter Archives
- Video Courses
- Excel Products
- Windows Products
- Word Products
- Beauty and Style
- ExcelTips (Excel 97–2003)
- ExcelTips (Excel 2007–2021; 365)
- Home Improvement
- Money and Finances
- Pests and Bugs
- Pets and Animals
- WindowsTips (Microsoft Windows)
- WordTips (Word 97–2003)
- WordTips (Word 2007–2021; 365)
Copyright © 2024 Sharon Parq Associates, Inc.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
How to add a button to a Word document and assign its Click event at run-time
- Applies to: Microsoft Word
This article demonstrates how you can use a Microsoft Visual Basic for Applications macro to programmatically add a control to a Microsoft Word document and add a Click event handler for that control.
More Information
The following steps illustrate how you can create a Word macro that will add a control to a document and assign the Click event of that control at run-time. The steps are for Word. However, you can apply the same concepts to programmatically manipulate controls in Microsoft Excel workbooks.
The ability to manipulate the Visual Basic Project of a Microsoft Office document at run-time requires a reference to the Microsoft Visual Basic for Applications Extensibility library.
Steps to create the sample
Start a new document in Word.
Press Alt+F11 to go to the Visual Basic Editor.
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.
Run the macro "Test".
Once the macro "Test" finishes running, you will see a new CommandButton control on a new document. When you click the CommandButton control, the Click event of the control fires.
Additional notes for Word 2002 and Word 2003
By default, access to a Word VBA project is disabled. When disabled, the code above may generate the run-time error '6068', "Programmatic Access to Visual Basic Project is not trusted." For more information about this error and how you can correct it, click the following article number to view the article in the Microsoft Knowledge Base:
282830 Programmatic access to Office VBA project is denied
Additional resources
How to Assign a Macro to a Button in a Word Document
These buttons have a macro or Word command assigned to them and you can control what happens when the button is clicked in your text. And this is all because you are using a field code!
We really should not refer to the macro as a “button,” because there is no graphic or icon involved. You can, however, create your own graphic and embed it into your field. That field code syntax would be:
MacroButton MacroName Display
MacroName is the name of the macro or command you would like to urn and Display is the text you would like displayed by the field code. If you use a graphic rather than text, then the graphic is displayed. When you double-click on the displayed text or graphic, then the macro or command defined by MacroName will be executed.
Follow the steps below to learn how to assign a command or macro to a button in your text:
- Position your cursor where you would like the MacroButton to appear.
- Click CTRL + F9 to insert a field.
- Between the field brackets, key in the field name MacroButton and a space).
- Key in the name of the command or macro you want the button to executed.
- To find the correct macro or command names, you can right-click on a blank area of your Ribbon and click on Customize Quick Access Toolbar . You can the names in the dialog box.
- Key in the button name text you want displayed or inspect a graphic to be used.
- Click F9 to update the field display.
About The Author
Carol Bratt
1 thought on “how to assign a macro to a button in a word document”.
Carol I love your tips đ You make me look like a genius at work every time I use one of your tips. I need to just have a folder for all these. Keep these coming!
Comments are closed.
WHY NOT SUBSCRIBE TO OUR NEWSLETTER?
Get great content like this delivered to your inbox.
It's free, convenient, and delivered right to your inbox! We do not spam and we will not share your address. Period!
IMAGES
VIDEO
COMMENTS
1. Click where you want to use a button, and then press Ctrl +F9 key to insert a pair of braces, and then type the below text between the braces, see screenshot: MACROBUTTON MacroName Double-Click To Run Macro Note: MacroName is the code name in your Word file that you want to run when clicking the button, please change it to your need. 2.
Microsoft security Accessibility center Record or create a macro and run it with a button or keyboard shortcut.
Press Ctrl+F9 to insert a pair of field brackets. Between the field brackets, type the fieldname MacroButton followed by a space. Type the name of the command or macro you want the button to execute, followed by a space. (This is the name you determined in step 1.)
The following steps illustrate how you can create a Word macro that will add a control to a document and assign the Click event of that control at run-time. The steps are for Word. However, you can apply the same concepts to programmatically manipulate controls in Microsoft Excel workbooks.
Click CTRL + F9 to insert a field. Between the field brackets, key in the field name MacroButton and a space). Key in the name of the command or macro you want the button to executed. To find the correct macro or command names, you can right-click on a blank area of your Ribbon and click on Customize Quick Access Toolbar.