![]() |
|
|||||||||
| |
||||||||||
|
Training > Tutorials > Word AutoForms > Trigger Your Word Docs |
||||||||||
|
|
Trigger Your Word Docs
This article originally appeared in All 'Bout Computers Ezine Have you ever wished that you could put a trigger into a document or report that would help users by causing some type of event to happen? Think about it. You have a report template that you need users to complete. But some of the information in the document is a bit obscure. You’d like to add a little extra information to explain what is required, but you don’t want to have the report appear too complex due to a lot of additional explanation text. Text that more advanced users might find annoying. How can you cover both bases—keep the document clean for the advanced users, but provide that extra assistance for new users? By adding a little VBA code, you can slip in little triggers to provide the new users with ways to get the additional help they need. MacroButton Triggers I added in all the section titles that were needed. Then I converted the title text into MacroButtons. A macrobutton is text or an image that, when double clicked, will trigger a macro. Note, however, that additional code can also be added that will convert the default double click for MacroButtons to single click activation. As you can see in the final result below, if the user clicks on a section title, a dialog box now appears that provides additional details.
Showing the users that they just need to click any title for more information turned out to be a much easier way for them to work. To create a macrobutton, you first need to write some type of actual macro. In this case, I created a dialog box to display. But I could have just used a message box.
A display macro was created for each dialog box that simply tells the dialog box to show itself. frm_06ConceptDefinitions.Show And a hide command was placed under the OK button, to cause the dialog box to go away and remove it from memory when the user dismissed the message. frm_06ConceptDefinitions.Hide Once the macros were created, I added the MacroButton field code to the document to trigger the macro to run, and thereby cause the help dialog box to be displayed. You can build the MacroButton field manually by hitting Ctrl + F9 to insert the Word field brackets. Then construct the needed code using the following syntax:
Or you can click Insert > Field > MacroButton and fill in the needed information through the Word user interface. If you need more help understanding how to use a MacroButton field, check Word’s Help or see this TechTrax article, which explains how to add an envelope icon to a page to trigger an envelope to print: Instant Envelopes Using the MACROBUTTON Field and VBA. ActiveX Command Control Automated forms can be programmed to start running immediately when the user accesses the file. However, in some cases, this isn’t always the best way to develop a form. Once I had to create a license request form. But some users wanted to just check the cost of a license before they went whining to their boss to beg for a software purchase. So I added command buttons to the form to allow the users to decide how they needed to use the form. If they just wanted to check a price, they would click that button. However, if they already had approval to submit the request form, they could go ahead and click the other button to start the form fill-in activation, as you can see in the image below.
The only problem with using ActiveX Controls is that, if you get carried away and use too many, it will add a lot of excess weight to your document. Weight that could cause the document to open slowly or even crash! Use them sparingly. If you only need two or three triggers, you can get away with using command buttons. But if you need several triggers, a MacroButton is much less weight on your document. To access the Control Toolbox that contains the ActiveX Controls, click View > Toolbars and select the Control Toolbox.
These controls look very similar to the ones you’ll find on the Forms toolbar. However, Form Fields can be used without the need of VBA programming code. ActiveX Controls need code to activate them. Also, as I mentioned, they add a lot more weight to your document.
Click the Command Button control from the Control Toolbox to add a button to your document. Once added, right click to access the Properties dialog for the control. This is where you configure the command button to look and behave the way you want. As you can see, I’ve added the Caption "Help," as well as adjusting the font size for the button text display.
By making the font smaller, I can now click and drag a corner of the button so it doesn’t take up too much space on the document.
Code needs to be added to the control to cause something to happen when the user clicks the button. In this case, I’ll simply add a message box that will be displayed when the user clicks the command. If I’d created a dialog box and wanted that to display, this is where I would add my .show command to cause the dialog box to be displayed.
Now when the user clicks on the Help button in the document, my message is displayed, as you can see below.
Removing Controls I’ve seen some elaborate code procedures that can be used to remove controls. And a Microsoft article explains that you can turn off the Drawing Object printing option from the Tools > Options > Print dialog box. But what if you have other drawings in the document that need to be printed? A simple solution is to just wrap the control with a bookmark! Select the command button and hit Alt + I + K to add a bookmark around the control.
Now you can write a simple procedure that changes the content of the bookmark to nothing…thereby removing the control from the page prior to printing. The code below would set a bookmark called "bkButton01" to nothing by entering two double quotes with no information between them. ActiveDocument.Bookmarks("bkButton01").Range.Text = ""
Of course, now you’ll have to trigger this procedure to run at some point. You can capture the print command and add code to set all the control bookmarks on your page to nothing. Or you can add another command button that also does the trick. Just remember that you’ll be adding even more weight to your document if you choose to add yet another command button.
If your users understand how to pay attention to custom menus, you can add a command to your menu bar for this document that triggers the code to remove the buttons, as shown below.
When the users click this menu option, the procedure to set all the bookmarks to nothing will run, thus removing the buttons, automatically. If you need further help learning how to customize Toolbars, see this MouseTrax article: Customized Toolbars and Menus. If you want more help learning how to create custom dialog boxes, check out this TechTrax article: Creating Custom Dialog Boxes. If you’d like to learn more about automating documents in Word by using VBA, be sure to check out my Word AutoForms & Beginning VBA eBook or Video course at this link: http://www.mousetrax.com/techcourses.html. And if you’d prefer to have an expert show you all the ways you can save time and money by using automated documents in your business, see my consulting page at: www.mousetrax.com/consulting. |
|||||||||