The Word VBA object model is the hierarchy of objects, properties and methods that Word exposes to macros, and you use it by opening the VBA editor with Alt+F11, inserting a module, and writing procedures that reference objects like Application, Documents, Range and Paragraph. Once you understand that hierarchy, you can automate formatting, find and replace, document generation and repetitive editing without touching the interface by hand. You do not need any add-in or extra software: everything ships with Word, though you must enable the Developer tab (File > Options > Customize Ribbon, then tick Developer in the right-hand list) to reach the tools easily. Menu labels can differ slightly by version and region, but the structure described here is stable across modern desktop versions of Word for Windows.
What You'll Need
- Microsoft Word for Windows or Mac, desktop version (the VBA editor is not available in Word for the web or most mobile versions).
- The Developer tab enabled, or at minimum the keyboard shortcut Alt+F11 to open the editor.
- Macro security set to allow your own code: File > Options > Trust Center > Trust Center Settings > Macro Settings. Choose the setting that disables macros with notification so you can approve your own document.
- A scratch document you do not mind changing, saved as .docm if you want your macros to persist.
- Basic familiarity with variables and loops in any language. If you have none, the Object Browser inside the editor is your best teacher.
Step-by-Step
- Open the VBA editor. In Word, press Alt+F11. On a Mac, use Tools > Macro > Visual Basic Editor. A separate window opens with a Project pane on the left listing open documents and templates.
- Add a standard module. In the editor, choose Insert > Module. A blank code window appears. Code placed here is general-purpose; code placed in the ThisDocument module belongs to that specific document and runs on its events, which is a different job.
- Learn the top of the hierarchy. Everything begins with
Application, which represents Word itself. From it you reachDocuments(the collection of open documents), then an individualDocument, then itsRange,Paragraphs,Tables,Bookmarksand so on. Each collection holds items you can count and loop through. - Write a first macro that references the object tree. Type:
Sub CountWords()MsgBox ActiveDocument.Words.CountEnd Sub
Place the cursor inside the procedure and press F5. The message box shows the word count of the active document. This single line demonstrates the core pattern: an object (ActiveDocument), a property (Words), and a further property (Count). - Loop through a collection. Collections are the workhorse of the model. Replace the code with a loop over paragraphs that reports how many are empty:
Sub CountEmpty()Dim p As Paragraph, n As LongFor Each p In ActiveDocument.ParagraphsIf Len(Trim(p.Range.Text)) <= 1 Then n = n + 1Next pMsgBox n & " empty paragraphs"End Sub
Note that each paragraph's text includes its paragraph mark, which is why the length check uses 1 rather than 0. - Understand Range versus Selection.
Selectionis wherever the cursor or highlighted text sits and depends on what the user is doing.Rangeis a defined span of the document you control explicitly, for exampleActiveDocument.Range(0, 10)for the first ten characters. Professional macros rely on ranges, because they work without disturbing the user's cursor. Use Selection only for interactive macros triggered by the user. - Modify content safely. Ranges and styles are the standard way to change formatting. To make the first paragraph of a document bold and larger, capture it in a range variable first, then set properties on that range rather than re-selecting text. Apply built-in styles (such as Heading 1) instead of hard formatting where possible, because styles keep documents consistent and make later bulk edits far easier.
- Add documents and work across files. Use
Documents.Addto create a new document andDocuments.Openwith a full file path to open an existing one. Assign the result to a variable:Dim d As DocumentthenSet d = Documents.Add. Objects require theSetkeyword; forgetting it is the single most common beginner error. Close what you open withd.Close SaveChanges:=wdSaveChangesorwdDoNotSaveChanges. - Use Find and Replace through the object model. The
Findobject on a range gives you scripted search. Set.Textand.Replacement.Text, set.Forward = Trueand.Wrap = wdFindContinue, then call.Execute Replace:=wdReplaceAll. Wrap the whole thing in aWithblock to keep the code readable. This is the reliable way to clean up imported text at scale. - Explore with the Object Browser. Press F2 in the editor. Select the Word library in the top dropdown, then browse every object and its members. Press F1 on a highlighted member for its help page. This is faster and more accurate than guessing property names, and it shows you which constants belong together.
- Save and run it again later. Save the document as a macro-enabled file (.docm). To run the macro without the editor, use View > Macros (or Alt+F8), select the name, and click Run. Assigning it to the Quick Access Toolbar makes it a one-click button.
- Turn on Option Explicit. At the top of the module, add
Option Explicit, or enable Require Variable Declaration under Tools > Options in the editor. This forces you to declare every variable and catches typos that would otherwise produce silent, confusing behaviour.
Common Mistakes
Forgetting Set when assigning an object is the classic error: without it you get a runtime error about an object variable not being set. Referencing the wrong object is the second: ActiveDocument fails when no document is open, and Selection behaves unpredictably if the user clicks elsewhere mid-macro. Editing text while looping forward through paragraphs shifts the collection and skips items; loop backwards or collect items first. Hard-coding file paths breaks as soon as the file moves, so build paths from a folder variable. Finally, leaving the screen updating on during a long loop makes macros appear frozen; set Application.ScreenUpdating = False at the start and restore it to True before finishing, including in error handling.
Tips
Record a macro first (Developer > Record Macro), then read the generated code. It is verbose and clumsy but it reveals the exact object names for actions you want to automate. Use the macro recorder as a dictionary, not as finished code. Declare variables with meaningful names and short scopes, and group related operations in small procedures you call from one main routine. Test on a copy of a real document, never the original. Add a simple error handler, On Error GoTo Cleanup, with a label at the end that restores screen updating and closes open files. When a macro crashes, press the Reset button in the editor before running again, otherwise stale variables linger and produce misleading results.
Troubleshooting
If nothing happens when you press F5, check the cursor is inside a Sub and that the module is not marked as a document module expecting event parameters. If you get a security warning, your Trust Center settings are blocking macros; adjust them deliberately rather than lowering protection globally, and only run code you understand. If a macro runs but does nothing to the visible document, it may be acting on a range you created but never inserted content into, or on a different open document than the one you expect, so check which document is active. If Word freezes, press Esc and then stop the macro with Ctrl+Break. If results look wrong after several runs, reset the code, close and reopen the document, and confirm your loop is not reprocessing text it already changed. If macro buttons stop working after moving a file, remember .docx strips macros entirely; resave as .docm.
Frequently Asked Questions
Do I need to be a programmer to use the Word object model?
No, but you need patience. Recording macros and reading the generated code teaches you most of the object names you will need for everyday tasks.
Does this work in Word for the web?
No. The VBA editor is only in the desktop versions of Word for Windows and Mac. Documents containing macros still open in the browser, but you cannot edit or run new code there.
What is the difference between Application and ThisDocument?
Application refers to Word as a whole. ThisDocument refers to one specific document and is where you put code that must run automatically when that document opens or closes.
Why does my macro stop working after I save as .docx?
Because .docx cannot store macros. Save as .docm to keep your code and run it again later.
Can I undo everything a macro did?
Only in part. A macro's changes are usually undoable with Ctrl+Z, but long procedures may be grouped into a single undo step or not recorded at all. Always run new code on a copy.
How do I find the exact name of an object or property?
Use the Object Browser (F2) in the editor and filter to the Word library. It lists every member, and F1 opens the official help page for the selected item.