Macros

We use a few dozen macros regularly for editing in Word and for working in Excel and PowerPoint. Most of these have either been hand coded by Michael or are modified versions of others’ macros. As a service to the writing and editing community, we will be providing some of these macros here, along with links to any related blog posts. If you have any feedback, feel free to drop us a note. And if you like our macros, please feel free to buy us a cup of coffee!

Disclaimer: We are providing these macros and associated information without a warrantee of any kind. Users assume all responsibility for use of these macros. Always make sure that you understand how a macro functions before using it. Test each macro before running it on an important document, and always make sure that your Normal.dotm template and the document a macro is run on are backed up properly.

Add / Delete AutoCorrect Entries Based on a List

The two macros provided — ImportAutoCorrect and DeleteAutoCorrect — take content from a Word document and copy it into or delete it from Word’s AutoCorrect list. These macros are NOT designed for creating formatted or multiparagraph entries; instead we recomment using the AutoText macros available in a macro-enabled Word document provided below under “Add / Delete AutoCorrect and AutoText Entries.”

  • Content to be inserted into or deleted from Word’s AutoCorrect list should be placed in a dedicated Word source document; the macros are run with this document as the active document.
  • Each AutoCorrect entry in the source document should be in its own paragraph. Content in this paragraph should consist of the information to appear in the “Replace:” column, a tab character, and the content to appear in the “With:” column. For example:

+cor     Is this revision correct?

  • Every paragraph that contains a tab character will be treated as a potential AutoCorrect entry. If these don’t present information in the format given above the result may be an an error message or unintended content being added to the AutoCorrect list.
  • Paragraphs that do not contain a tab character are ignored and can be used to include comments or other information in the Word document.
  • DeleteAutoCorrect removes AutoCorrect list entries if a matching name (replace value) is located in the source document.
  • AutoCorrect entries have a 255-character limit, and a longer entry will result in an error message.
  • The DeleteAutoCorrect macro requires the function ExistsInCollection(), which is included.

This file was updated February 17, 2022 to skip any AutoCorrect string that is longer than 255 characters and alert the user.

Click here to view the ImportAutoCorrect and DeleteAutoCorrect macro code or right click and select “Save link as…” to download the macros as a text file

Add / Delete AutoCorrect and AutoText Entries (macro-enabled document)

This macro-enabled Word document is a template for storing standard queries that can then be importing en mass into Word’s AutoText or AutoCorrect libraries using the embedded macros ImportAutoTextQueries or ImportAutoCorrect. Deleting these entries en mass can then be done using the embedded macros DeleteAutoTextQueries or DeleteAutoCorrect.

  • This is a macro-enabled Word document and you may need to mark it as a trusted document in order for the macros to be operable.
  • This document includes the macro code available elsewhere on this page under the heading “Add / Delete AutoCorrect Entries Based on a List.”
  • Instructions for using the macros and populating the query lists appear on the first page of the document.
  • To facilitate testing of the macros, the file contains example queries organized using headings based on common editing topics and Chicago Manual of Style chapter numbers.
  • When the file is open, buttons to run the macros will appear on your Quick Access Toolbar, as will icons to open the AutoText and AutoCorrect dialogue boxes. Links to run the macros also appear on the first page of the document.

Right click and select “Save link as…” to download the macro-enabled .docm file

Adjust Highlighting

This dialogue box-based Word macro allows the user to remove specific highlighting colors from a selection within the main body of a Word document.

  • Because this macro uses a dialogue box, installation involves importing a few macro and form files. Everything needed (including installation instructions) is provided in a .zip folder.
  • The macro removes the selected highlighting colors from the current selection; if no selection has been made when the macro is run, it will ask whether you want the whole document to be set as the selection.
  • If track changes is turned on you will be given the option to turn it off for the highlighting removal.

Example Image: Dialogue box with default websites

Right click on this link and select “Save link as…” to download the files in .zip format

Copy Words to Style Sheet

This macro, based on the simple StyleThat macro written by Hilary Powers and a later adapted version of that macro by Jack M. Lyon, copies a selection from one Word document to another and provides some formatting appropriate for a style sheet. A blog post discussing an earlier version of this macro is available here.

  • It operates if two and only two Word documents are open.
  • If it finds a heading consisting of “Word List” (the value of the variable “sSectionTitle”) it places the selection directly after the heading paragraph. You can create different versions of this macro with different sSectionTitle values to paste directly into different sections.
  • If it finds no matching heading, it pastes the content at the end of the document.
  • All formatting of the pasted content is removed, but if the entire selection is in italics, this formatting will be restored.
  • The macro inserts brackets after the pasted content and places the cursor between them, to simplify adding a description.
  • Running the macro a second time (with nothing selected) returns focus back to the source document.

Please note: I use a highly customized version of this macro tailored to my specific style sheet and am not currently maintaining this code, so please report any bugs you find.

Click here to view the CopyWordstoStyleSheet macro code or right click and select “Save link as…” to download the macro as a text file

Count Words by Chapter

This macro identifies chapter names, starting page numbers, and word counts from a manuscript based on the chapter title heading style. It then creates a new Word document containing a summary. A blog post discussing an earlier version of this macro is available here.

  • If text has been selected when the macro is run, its paragraph style name will be extracted as the heading style the macro will use. If text has not been selected the user is prompted to enter a style name.
  • Data about the source file and export process will appear in the header of the report (created by code from the macro ExtractCommentsToNewDoc by Lene Fredborg).
  • The word count reflects visible content, so the number for a document with tracked changes will vary based on the visibility of the changes.
  • An entry of “[Before first heading]” will appear if the document does not start with a chapter heading.
  • An entry of “[No chapter title]” will appear if the title of a section is just a hard return character. Content such as page and section breaks formatted with the heading style will appear in the final report, possibly leading to formatting issues.
  • Tabs appearing in chapter titles are replaced with spaces to facilitate report formatting.
  • This macro uses the subroutine ClearFindAndReplaceParameters (included), which is necessary for it to run properly.

Click here to view the CountChapterWords macro code or right click and select “Save link as…” to download the macro as a text file

Extract Comments for Read Aloud

This macro, based on ExtractCommentsToNewDoc by Lene Freborg, is tailored to allow Word’s Read Aloud feature to be used to review text from comments. (Using Read Aloud for proofreading is a handy technique for identifying easily overlooked errors.)

  • Only the page number and comment text are included.
  • The display text for any URL beginning with “http” is shortened to “[URL Replaced]” but the link is maintained.
  • Hard returns within comments are replaced by soft returns for the purpose of formatting.
  • Page numbers reflect the pagination with tracked changes not visible.
  • This macro has not been tested with Word’s Modern Comments “feature” and will not be tested in the foreseeable future.

Click here to view the ExtractCommentsForReadAloud macro code or right click and select “Save link as…” to download the macro as a text file

Find First Occurrence in a Second Document

This macro takes a selection from one Word document (for example, a style sheet) and looks for its first appearance in the body of a second document (for example, a manuscript). It is based on the StyleThat macro written by Hilary Powers and a later adapted version of that macro by Jack M. Lyon.

  • This macro will only function properly if two Word documents are open.
  • This macro also puts the selected term on the clipboard to facilitate searching for other appearances of the term.

This file was updated July 19, 2022 to add the missing ClearFindAndReplaceParameters subroutine

Click here to view the FindFirstOccurrence macro code or right click and select “Save link as…” to download the macro as a text file

Highlight All Term Appearances

When a word or phrase is selected and this macro is run, it will highlight in light gray all whole-word, identically capitalized instances of that content in the body of the document.

  • Formatting such as italics is ignored.
  • The macro will not necessarily perform as expected with content that has capitalization applied via a Word style or using Word’s Change Case tool or with content containing tracked insertions or deletions.
  • Only content in the current Word “story” will be highlighted; highlighting does not take place in headers and footers, end notes or footnotes, text boxes, or other portions of the document.
  • If nothing is selected, the macro will select the word that the cursor is currently placed within or adjacent to. The macro also contains commented-out code that can be substituted that will instead alert you if the selection is empty.
  • The macro is designed to include possessive versions of words, but the tradeoff is that it can return false positives. If this is problematic the code can be revised to force whole-word selection using a wildcard search.

Click here to view the HighlightBodyText macro code or right click and select “Save link as…” to download the macro as a text file

Highlight Selection

This suite of macros highlights the selection in one of a range of colors, with the option of turning tracking on or off on a color-by-color basis. If you do not need to control tracking, a much shorter macro can provide highlighting in a specific color, though it will not provide significantly better performance.

  • The code contains six macros to apply highlighting in cyan, green, pink, red, yellow, and no color. Each of these macros passes values to the HighlightWithColor subroutine, which does the actual highlighting and must be present.
  • To highlight in additional colors, replicate one of the macros, changing the name and the WdColorIndex enumeration value.
  • By default the macros base tracking on the current Track Changes setting. However, if the “Tracking” parameter in a macro is changed to “True” highlighting in that color will always be tracked. If set to “False” highlighting in that color will never be tracked.
  • If no selection exists when the macro is run, the word the cursor is within or adjacent to will be selected.

Click here to view the HighlightWithColor macro code or right click and select “Save link as…” to download the macro as a text file

Insert Comment Tags (AutoHotKey script)

This AutoHotKey script allows the user to quickly select and insert predefined, highlighted tags into Word comments via popup menus. The comment that is created is applied to the current selection. If the cursor is in a comment and nothing is selected, the entire comment will be replaced by the selected tag. If the cursor is in a comment and content is selected, only that selection will be replaced by the tag.

  • NOTE: This script was designed to work with traditional Word comments and has not been tested with modern comments.
  • To run the script, AutoHotKey must be installed. See the beginner tutorial for information on downloading AutoHotKey and loading a script.
  • The script comes with code for two sample pop-up menus, which are opened with the shortcuts Alt + y and Alt + u.  The user will need to modify the script code to adjust the tags, keyboard shortcuts, menu colors, and highlighting colors.
  • Additional menus can be created by copying and editing the appropriate portions of the script.
  • To unload the script, find its icon in your Windows system tray, right click on it, and select “Exit.”
  • The script can also be placed into your Windows Startup folder, which will load it automatically when Windows is opened.

Example image: Default pop-up menu

Example image: Comment created with the script

Click here to view the script code or right click on this link and select “Save link as…” to download the script as a text file

Trim Spaces in Excel

This Excel macro trims leading and trailing spaces from selected cells containing text (but not formula results) while leaving character-level formatting intact. It was built on the much simpler macro trimspace.

  • Both regular spaces and nonbreaking spaces are deleted.
  • If a cell contains only spaces, these are deleted.
  • Cells that are adjusted are shaded in green; the macro also contains commented-out code to add a green border.
  • Cells containing text longer than 255 characters are not edited but are shaded in yellow if leading or trailing spaces are found; the macro also contains commented-out code to add a red border.
  • Please note: Excel stores macros differently than Word. See this page for more information.

Click here to view the TrimLeadingAndTrailingSpaces macro code or right click and select “Save link as…” to download the macro as a text file

Web Search Launcher

This dialogue box-based Word macro, based on Paul Beverly’s GoogleFetch macro, uses the current selection as the search string for one of a number of websites selected via a dialogue box.

  • Because this macro uses a dialogue box and a configuration file, installation involves importing a few files into Word and adding content to the Word template folder. Everything needed (including installation instructions) is provided in a .zip folder.
  • This macro requires a minimum of two keystrokes/mouse clicks rather than one (as Paul Beverly’s macro does) but avoids the need to have a separate macro for each website.
  • The dialogue box allows the search string to be edited after a selection is made but before the search is performed.
  • The configuration file, accessible from within the dialogue box, allows the website list to be quickly adjusted.
  • The AutoHotKey script WebSrcLauncher, available on this page, provides the same general functions as this macro but works in any Windows program. However, once installed this macro is easier to adjust and provides features WebSrcLauncher does not have.

This file was updated June 19, 2021 to add missing content.

Example Image: Dialogue box with default websites

Right click on this link and select “Save link as…” to download the files in .zip format

WebSrcLauncher (AutoHotKey script)

This AutoHotKey script, inspired by Paul Beverly’s GoogleFetch macro, takes the current selection and uses it as a search string on a website selected from a pop-up menu. Unlike the various fetch macros, which only run in Word, this script will work in any Windows program that allows you to copy content to your clipboard. The menu is prepopulated with some websites used by editors, but this list is fully editable.

  • To run the script, AutoHotKey must be installed. See the beginner tutorial for information on downloading AutoHotKey and loading a script.
  • Once loaded, the script is run using the keyboard shortcut Alt + `. Instructions for changing this shortcut are available in the beginner tutorial.
  • The list of websites in the menu can be adjusted by changing the website names in the menu section of the script and the corresponding URLs, which include search strings, in the case section of the script.
  • To unload the script, find its icon in your Windows system tray, right click on it, and select “Exit.”
  • The script can also be placed into your Windows Startup folder, which will load it automatically when Windows is opened.

Example image: Default pop-up menu

Click here to view the script code or right click on this link and select “Save link as…” to download the script as a text file