gregmaxey.com
Open in
urlscan Pro
198.187.28.133
Public Scan
Submitted URL: http://gregmaxey.mvps.org/word_tip_pages/installing_employing_macros.html
Effective URL: https://gregmaxey.com/word_tip_pages/installing_employing_macros.html
Submission: On September 14 via manual from AU — Scanned from DE
Effective URL: https://gregmaxey.com/word_tip_pages/installing_employing_macros.html
Submission: On September 14 via manual from AU — Scanned from DE
Form analysis
8 forms found in the DOMPOST https://www.paypal.com/cgi-bin/webscr
<form class="PayPalVerDonateformEuro" action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_s-xclick">
<input type="hidden" name="hosted_button_id" value="776KL2DPCD4JN">
<input type="image" src="../images/Donate_Euro.png" name="submit" alt="PayPal - Pay with Euro">
</form>
POST https://www.paypal.com/cgi-bin/webscr
<form class="PayPalVerDonateformUSD" action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_s-xclick">
<input type="hidden" name="hosted_button_id" value="Z9Y7VQAJ7WNC8">
<input type="image" src="../images/Donate_USD.png" name="submit" alt="PayPal - Pay with US Dollars.">
</form>
POST https://www.paypal.com/cgi-bin/webscr
<form class="PayPalVerDonateformGB" action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_s-xclick">
<input type="hidden" name="hosted_button_id" value="HMGYMYSGL4WD4">
<input type="image" src="../images/Donate_GBL.png" name="submit" alt="PayPal - Pay with British Pound Sterling">
</form>
Name: Feedback — /website_feedback_contact.html
<form action="/website_feedback_contact.html" name="Feedback" class="Center_Text">
<input type="submit" value="User Feedback">
<script type="text/javascript">
document.write('<input name="FromURL" type="hidden" value="' + window.location.href + '" />');
</script><input name="FromURL" type="hidden" value="https://gregmaxey.com/word_tip_pages/installing_employing_macros.html">
</form>
POST https://www.paypal.com/cgi-bin/webscr
<form class="PayPalVerDonateformEuro" action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_s-xclick">
<input type="hidden" name="hosted_button_id" value="776KL2DPCD4JN">
<input type="image" src="../images/Donate_Euro.png" name="submit" alt="PayPal - Pay with Euros">
</form>
POST https://www.paypal.com/cgi-bin/webscr
<form class="PayPalVerDonateformUSD" action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_s-xclick">
<input type="hidden" name="hosted_button_id" value="Z9Y7VQAJ7WNC8">
<input type="image" src="../images/Donate_USD.png" name="submit" alt="PayPal - Pay with US Dollars">
</form>
POST https://www.paypal.com/cgi-bin/webscr
<form class="PayPalVerDonateformGB" action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_s-xclick">
<input type="hidden" name="hosted_button_id" value="HMGYMYSGL4WD4">
<input type="image" src="../images/Donate_GBL.png" name="submit" alt="PayPal - Pay with British Pound Sterling">
</form>
GET https://www.google.com/custom
<form action="https://www.google.com/custom" method="get" target="_top" class="GoogleSearch" style="width: 618px">
<table class="SrchTable">
<tbody>
<tr>
<td class="nowrap" align="left">
<img alt="Google Search Logo" src="../images/Logo_25gry.gif" width="75" height="32">
</td>
<td>
<input name="domains" type="hidden" value="gregmaxey.com">
<label for="sbi" class="GoogleSearchLabel">Enter your search terms</label>
<input id="sbi" maxlength="255" name="q" size="45" class="SrchInput">
<label for="sbb" class="GoogleSearchLabel">Submit search form</label>
<input id="sbb" name="sa" type="submit" value="Search">
</td>
</tr>
<tr>
<td></td>
<td><input id="ss" name="sitesearch" type="radio" value="">
<label for="ss" title="Search the Web">Web</label>
<input id="ss1" checked="checked" name="sitesearch" type="radio" value="gregmaxey.com">
<label for="ss1" title="Search gregmaxey.com">gregmaxey.com</label>
<input name="client" type="hidden" value="pub-6857949530765071">
<input name="forid" type="hidden" value="1">
<input name="ie" type="hidden" value="ISO-8859-1">
<input name="oe" type="hidden" value="ISO-8859-1">
<input name="safe" type="hidden" value="active">
<!--BGC is background color 6 digit long.
<input name="cof" type="hidden" value="GALT:#940F04;GL:1;DIV:#E1771E;VLC:666666;AH:center;BGC:8395AD;LBGC:E1771E;ALC:191919;LC:191919;T:333333;GFNT:663333;GIMP:663333;FORID:1" /> -->
<input name="cof" type="hidden" value="GALT:#940F04;GL:1;DIV:#E1771E;VLC:666666;AH:center;BGC:D8DBE0; LBGC:E1771E;ALC:191919;LC:191919;T:333333;GFNT:663333;GIMP:663333;FORID:1">
<input name="hl" type="hidden" value="en">
</td>
</tr>
</tbody>
</table>
</form>
Text Content
The Anchorage Personal website of Gregory K. Maxey, Commander USN (Retired) * Quarterdeck * What's New * Microsoft Word Tips * Consulting * Chain Locker * Lucky Bag * Contact * Search Do you have ad-blocking software enabled? While I respect your right to do so, your donations and the minimal advertisements on this site help to defray internet and other costs of providing this content. Please consider excluding this website from blocking or turning off the blocker while browsing this site. INSTALL/EMPLOY VBA PROCEDURES (MACROS) (A MICROSOFT WORD HELP & TIP PAGE BY GREGORY K. MAXEY) DISCLAIMER/TERMS OF USE The information, illustrations and code contained in my "Microsoft Word Tips" are provided free and without risk or obligation. However, the work is mine. If you use it for commercial purposes or benefit from my efforts through income earned or time saved then a donation, however small, will help to ensure the continued availability of this resource. If you would like to donate, please use the appropriate donate button to access PayPal. Thank you! The purpose of this Microsoft Word Tips & Microsoft Word Help page is to show you a basic method to install and employ the many various VBA procedures (macros) that you will find in my collection of tips or in other Microsoft Word Help forums. The VBA procedure (procedure, routine or macro) shown below performs the common and often requested task of updating document fields. It can be used in all versions of Word (Word 97 through 2013). We will use it as a representative example macro in the discussion that follows. VBA Code: Copy to clipboard Public Sub UpdateAllFields() Dim rngStory As Word.Range Dim lngValidate As Long Dim oShp As Shape Dim oToc As TableOfContents, oTOA As TableOfAuthorities, oTOF As TableOfFigures lngValidate = ActiveDocument.Sections(1).Headers(1).Range.StoryType For Each rngStory In ActiveDocument.StoryRanges 'Iterate through all linked stories Do On Error Resume Next rngStory.Fields.Update Select Case rngStory.StoryType Case 6, 7, 8, 9, 10, 11 If rngStory.ShapeRange.Count < 0 Then For Each oShp In rngStory.ShapeRange If oShp.TextFrame.HasText Then oShp.TextFrame.TextRange.Fields.Update End If Next End If Case Else 'Do Nothing End Select On Error GoTo 0 'Get next linked story (if any) Set rngStory = rngStory.NextStoryRange Loop Until rngStory Is Nothing 'Special Cases For Each oToc In ActiveDocument.TablesOfContents oToc.Update Next oToc For Each oTOA In ActiveDocument.TablesOfAuthorities oTOA.Update Next For Each oTOF In ActiveDocument.TablesOfFigures oTOF.Update Next Next lbl_Exit: Exit Sub End Sub INSTALLING MACROS (BASIC METHOD): To use this or any VBA procedure (i.e., macro) it must be installed in a VBA project that is available to the document. To install and employ this macro perform the following steps: * Press ALT+F8 to open the "Macros" dialog. * Type in the macro name. Just the name, not the procedure type (Sub), no spaces, and not the "()." Note: If the code you have copied from a support forum or other source is just a snippet (i.e., not a complete named procedure) then simply type in any meaningful name for the macro you create. * Select Normal (global template) in the "Macros in:" field. Note: Macros you intend to use for a specific document type (e.g., invoice, letterhead, etc.) should be stored it your template that you create those documents from. General macros that you want to be available to all documents should be stored in a global template. Here, and for the purpose of this example only, I have suggested the Normal template. See the section Organizing Your Macros/Template Add-Ins below for more information. * Click "Create" Note: In this example I am using Word 2007. The dialog is similar in all Word versions. * After clicking "Create," the Visual Basic Editor (VBE) opens. The VBE is a powerful tool and serves as the control panel for practically everything you will learn to do with macros or VBA. The VBE that opens should look similar to the illustration below. Notes: 1. If not displayed, press CTRL+r (or use the VBE "View" menu and select "Project Explorer") to display the top left "Project" pane. 2. If not displayed, press F4 to do display the bottom left "Properties" pane. 3. Use the drag bar between the panes to adjust as appropriate. 4. The large white panel on the right is the "Code" pane. 5. The macro is created in a new standard module "New Macros." The module is stored in the modules folder as part of the Normal template. As you realize the benefits of macros and install more or begin creating your own you will want to organize your macros for more efficient management. See the section Organizing Your Macros/Template Add-Ins below for more information. * As code provided in my tips pages includes the procedure type (e.g. Sub), name, and End statements, select everything as shown below and delete it. Note: If the code you have copied from a support forum or other source is just a snippet (i.e., not a complete procedure) then paste your snippet in between the procedure type/name declaration statement and the End Sub statement. * Then copy the UpdateAllFields procedure shown above and paste it in the VBE code pane below the Option Explicit statement (if present). * Save the project using the "Save" toolbar command or the File>Save Normal command or CTRL+s. * Close the VBE by clicking the red "X" at the top right corner of the editor. Note: The purpose of the "Option Explicit" statement is beyond the scope of this tips page. If it is there leave it, if it is not don't worry about it. If you are like me and just have to know what it is all about then see: Declaring Variables/Constants * That's it. Your macro is installed and ready for use!! TROUBLESHOOTING It didn't work! I followed all of your steps but when I pasted the macro code in the code window the result looks something like this: After pasting code, any lines that appear in red are usually an indication that one or more lines of code has broken prematurely. This can result from line wrapping in the e-mail editor or other source editor. This issue can be fixed by joining the broken line in one of two methods: * Put your cursor at the end of the first broken line segment and press backspace. * Insert a line continuation character "_" at the end of the broken line segment. Both methods are illustrated below. The line continuation character is often used to minimize run on code lines and make code easier to read: INSTALLING MACROS (QUICK METHOD): There is almost always more than one way to skin a cat. The basic method should be fool proof, but as you gain familiarity with the VBE you will discover other methods. For example, I typically bypass the "Macros" dialog (ALT+F8) altogether and go straight to the VBE using the shortcut ALT+F11. From there: * Locate and select the project in the VBE Project Explorer window. * Use the VBE "Insert" menu to insert a new standard module. * Select the new module and use the Properties Window "Name" property to give my new module a meaningful name (e.g., I often name new single project modules modMain). * Paste the procedure into the main code pane. * I then immediately step through the new procedure using the F8 key or run the procedure using the VBE toolbar or menu commands. EMPLOYING MACROS: Now that your macro is installed to a global template it is ready for immediate use whenever you want to initiate it. Macros can be initiated in several ways. The most basic method is to use the same "Macros" dialog used to create the macro. * Press ALT+F8 to open the "Macros" dialog. * Select the macro you want to run and click "Run." Note: The illustration above shows the new macro with several of the macros that I've restored to my Normal template. * That's it!! You just executed the macro. With each version of Word there has been changes to the user interface (UI) and the location of the controls for working with macros. However, the ALT+F8 shortcut key specifically and the "Macros" dialog has remained consistent. It you think of the VBE as the "world" of VBA and macros (and it is), then you can think of the "Macros" dialog as the window to that world. Notes: 1. See my: Add Macro to QAT if you would like to add a macro to the Word 2007/2010 Quick Access Toolbar. 2. If you would like to see a more on using the UI controls associated with macros or how to run a macro using a keyboard shortcut, see MVP Graham Macro's: Installing Macros from Listings. 3. If you are still using Word 2003 and need assistance\instructions on how to run a macro from a menu or toolbar icon contact me via website feedback. ORGANIZING YOUR MACROS/TEMPLATE ADD-INS: If you are like most people, after you find/create and start using your first macro you may soon find yourself with dozens, if not hundreds or more. Save yourself some frustration and plan to organize your macros early so you can quickly find and use them when needed. Some basic suggestion for organization include: * Organize specialized macros that you use for a specific document type in the template that you base those documents on. * Organize any general macros that you want available to all documents in one or more global templates. The Normal template (.dot or .dotm) is a global template and it is always loaded. However, as the Normal template is susceptible to corruption, it is not the best place to store and keep macros. If you do, be sure (as with all important templates and documents) to keep a current backup. Creating one or more personal global templates to store and organize your macros it very simply. Just open a new Word document and save it as .dot (Word 2003) or macro-enabled .dotm (Word 2007 and higher). I recommend saving one in Word's startup folder to load automatically when Word starts for general macros that you use frequently (e.g., MyFrequentMacros.dotm) and another to your templates folder that you can loaded manually when you need it for the macros that you only occasionally or seldom use (e.g MyDustyMacros.dotm). * Give your macros meaningful names. * Use multiple standard modules with meaningful names within templates and add-ins to organize your macros by type or purpose. * Add frequently used macros to menus/toolbars (Word 2003) or the Quick Access Toolbar/Ribbon (Word 2007 and higher LOAD GLOBAL TEMPLATES/ADD-INS To load a global template add-in when Word starts, place the template in Word's Startup folder. Word's Startup folder can be determined using: * Word 2003 (and earlier) - Tools>Options>File Locations>Startup * Word 2007 - Office Menu>Word Options>Advanced>General>File Locations Button>Startup * Word 2010/2013 - File>Options>Advanced>General>File Locations>Startup Note: If you have not changed the preferred startup folder since installing Word, it can also be located (in English language versions of Windows) by typing %appdata%\Microsoft\Word\Startup In the Windows Explorer Address bar and pressing "Enter." To manually load a global template add-in, open the Templates and Add-ins dialog: * Word 2003 (and earlier) - Tools>Templates and Add-Ins... * Word 2007 (and later) - Developer tab>Templates>Document Templates When the dialog opens, click the "Templates" tab. The Templates and Add-ins dialog is similar in all Word versions and is shown below. Click the "Add" button, and using the "Add Template" dialog displayed, navigate to the template location and click "OK" Selecting template add-in to load Loaded template add-ins That's it! I hope you have found this tips page useful and informative. Share Facebook Twitter Google Bookmarks Email PAYMENTS/DONATIONS Do you want to make a payment for consulting work or donate to help support this site? PayPal is a safe, easy way to pay online. Use the appropriate currency "Donate" button to make a payment or donation. Search my site or the web using Google Search Engine Enter your search terms Submit search form Web gregmaxey.com This page was last updated on March 09, 2021 Website Privacy Statement Back to top Back to Word Tips ✓ Thanks for sharing! AddToAny More…