Word VBA

This is a collection of recipes for scripting Microsoft Word using Visual Basic for Applications.

Macro Recording
A great way of learning about Word VBA is using its macro recording function. With the function, you tell Word to start recording, then perform various steps as if you were working without a macro recorder, and finally, tell Word to stop recording. VBA code corresponding to what you did using Word GUI has been recorded by Word. While the code often cannot be meaningfully used without a modification, by starting from it and modifying it you can save a lot of time that would otherwise be spent reading the VBA documentation.

Menu paths:
 * Word 2007: View (tab) > Macros (group) > down-pointing triangle below Macros button > Record Macro
 * Word 2007: Developer (tab) > Code (group) > Record Macro

Links:
 * Record or run a macro (Word 2007) at microsoft.com
 * Create a macro (Word 2003) at microsoft.com
 * Recording a macro to generate code (Office 2000) at microsoft.com

Text Editing
You can insert and delete text as follows:

Moving Cursor
You can move cursor around as follows:

Selecting
You can select regions of text as follows:

Formatting
You can format text including text color, background color, and font properties as follows:

Copying and Pasting
You copy and paste as follows:

Clipboard
Prerequisites: Accessing the clipboard from a Word document requires that a reference to MSForms (Microsoft Forms Object Library) is set in the document. You can set the reference by adding and subsequent removing of a user form, via Insert > UserForm in a pop-up menu. To check the presence of a reference, see Tools > References menu.

Placing text on the clipboard:

Getting text from the clipboard:

Links:
 * DataObject Class at msdn.microsoft.com; contains a section on Visual Basic, whose applicability to Word VBA is unclear.

Related Wikibooks

 * Excel VBA
 * Visual Basic