Document automation - Word

This article contains tips and getting-started information for documentation automation with Microsoft Word.

MS Word has incredibly useful VBA features that allow you to automate routine documentation tasks. You can use the Record Macro feature to automate many repetitive actions, such as retagging paragraph styles, searching and replacing content, and so on. For advanced automation, use the Visual Basic Editor. The editor has many GUI features to assist you in writing code for complicated procedures.

Why automate?

Repetitive action is costly. How many hours does your documentation team spend performing the same actions over and over on the documents you produce regularly? When you analyze the time lost through such repetition, you can see how much more productive automation will make your team.

What is a Word macro?

Word stores automation code in the form of macros. Each macro contains the code necessary to perform a specific automation task. Often, macros are chained together to handle multiple taks.

You can view available macros from the Developer tab in the ribbon. Click the Macros button to view a list of macros available within your document.

Macros can be stored in individual documents, or in templates (.DOT files). I suggest storing them in templates, so that the macros will be available in all documents with the template attached.

The Macros dialog allows you to run, record, edit, organize, and step into macros.

Deciding what documentation tasks to automate

Some tasks are easier to automate than others. Here are a few quesions to help you decide if a task would benefit from automation.

  • Is the action you wish to automate exactly the same every time, or does each iteration require special consideration?
  • Can you summarize the steps involved in performing the action?
  • Do many people in your department perform the task regularly?
  • Does the task apply to many documents, or at least several long documents?

If you can answer Yes to most of these questions, chances are automation is worthwhile.

Recording a macro

Word allows you to record macros. This is the easy way to automate document tasks since it does not require you to write any code. Use the Record Macro feature on the Macros dialog to create a new macro. Then, perform the tasks that you wish to automate on the document you currently have open. Word will record your actions and write the macro code for you.

Click the Stop button when you are finished performing the tasks. Then, use the Run feature to test your new macro.

Recording macros and then editing them in the VBA editor is a great way to learn the VBA language. VBA is a flavor of the BASIC programming language, so it is easier to understand than many lower-level languages. Since you know what tasks you performed when recording, you can easily determine which chunks of code belong to each task.

Editing a macro in the VBA editor

Often the tasks you wish to automate are more complicated that what can be recorded in a single macro recording session. By working in the VBA editor, you can combine code from multiple recorded macros into a single, larger macro. You can also use the GUI tools to drag and drop code fragments for common document objects. You can also include more complicated code such as database connections and GUI forms for gathering information.

On the left side of the editor window, you'll see a list of the modules available in your template. You can store multiple macros in a module, or one per module. I recommend storing a single macro in each module to reduce confusion between module and macro names.

The editor also allows you to export your automation macros. However, for sharing and archiving purposes, it is often better to archive or share the entire template.

Each macro in the editor begins with a Sub statement and a name, and ends with an End Sub statement. All code between these statements belongs to that macro.

To include comments in your automation code, start the line with an apostrophe or a REM statement. Word will recognize this as a comment and will not try to execute it as code. Comments allow you to make important notes about the purpose of a block of code, so that you or others will have an easier time understanding it later.

As you develop more and more macros, you may find the Macros tab of the Organizer dialog useful. This dialog allows you to copy macros across document or template files. If you are sharing macros, it is best to just share the entire template, but you will find the Organizer tab useful for consolidating related macros and cleaning up your templates.

Wrapping up

As you continue recording and editing macros, you will find it easier and easier to understand the VBA language, and the types of features Word allows you to automate. Your document production process will become more efficient, and your bottom line will likely improve as a result.

Related post: Six Word macro tips you won't want to miss.