The Word VBA object model is the hierarchy of objects that Word exposes to macros: at the top sits Application, which contains Documents, each Document contains Range, Paragraphs, Tables, Styles and more, and every item in the document is reached by walking down that tree. Once you understand the chain, you can write a macro that edits, formats, finds, inserts or exports content with a few lines of code. This guide walks you from opening the editor to running a working macro that formats every heading and exports the file.

What You'll Need

  • A desktop copy of Microsoft Word for Windows or Mac (the editor and object model are not available in Word for the web or on mobile).
  • The Developer tab enabled, or the ability to press Alt+F11 (Windows) / Option+F11 (Mac) to open the editor.
  • A test document you do not mind breaking. Never develop macros on a file you care about.
  • Basic familiarity with variables, loops and the idea of an object having properties and methods. If you have never programmed, work through a short VBA primer first.

Step-by-Step

  1. Save a copy of your document first. Macros change files directly and there is no universal undo once code has run over hundreds of paragraphs. Keep an untouched backup in another folder.
  2. Open the Visual Basic Editor. In Word, press Alt+F11 on Windows. On Mac, use Tools > Macro > Visual Basic Editor, or Option+F11. A separate grey window opens with a Project pane on the left and a code area on the right. Menu labels can differ slightly by Word version and platform, but these paths are stable.
  3. Create a module for your code. In the editor, right-click your document's project in the Project pane and choose Insert > Module. A blank white code window appears. Modules hold general-purpose macros; the ThisDocument object is only for document-level event code.
  4. Turn on two habits that save hours. At the top of the module, type Option Explicit so Word forces you to declare every variable, and use the View > Immediate Window pane (Ctrl+G) to test single lines of code without running a whole macro.
  5. Learn the top of the hierarchy. Everything starts at Application. From there you reach the active file with ActiveDocument, or a specific one with Documents("Report.docx"). A useful trick for exploring: in the Immediate Window type ?ActiveDocument.Name and press Enter to print a property value. Replace Name with Paragraphs.Count, Tables.Count or Words.Count to inspect the document.
  6. Understand Range versus Selection. Selection is wherever the cursor is and follows the user; Range is an independent span of text you define in code. For reliable macros, work with Range objects rather than Selection, because the cursor can move or nothing can be selected when the macro runs. ActiveDocument.Content gives you a Range covering the whole document.
  7. Write your first working macro. Paste this into the module, replacing the sample body text with something from your own test file:
    Sub ListHeadings()
        Dim p As Paragraph
        Dim count As Long
        count = 0
        For Each p In ActiveDocument.Paragraphs
            If p.Style = "Heading 1" Then
                count = count + 1
                Debug.Print count & ": " & p.Range.Text
            End If
        Next p
        MsgBox count & " Heading 1 paragraphs found."
    End Sub
    This uses the Paragraphs collection, the Style property and a For Each loop, which are the three constructs you will use most.
  8. Run it and read the output. Press F5 with the cursor inside the subroutine, or use Run > Run Sub/UserForm. The Immediate Window shows the found headings and a message box gives the total. If you get a compile error instead, the offending line is highlighted in the code window.
  9. Edit text through a Range. To insert a line at the very start of the document, use ActiveDocument.Range(0, 0).InsertBefore "Draft - " & Format(Date, "yyyy-mm-dd") & vbCr. Note that vbCr is the paragraph mark; inserting vbLf creates a manual line break instead, which breaks heading styles and numbering.
  10. Work with collections by index or loop. Collections are 1-based in Word VBA: ActiveDocument.Paragraphs(1) is the first paragraph and ActiveDocument.Tables(1) is the first table. To change every heading, loop and set a property: inside a For Each p In ActiveDocument.Paragraphs loop, test p.Style and set p.Range.Font.Bold = True. Applying direct formatting is fine for a one-off, but changing the style definition (ActiveDocument.Styles("Heading 1").Font.Color = wdColorDarkBlue) is cleaner because it updates every use at once.
  11. Find and replace in code. The object is Content.Find, and it works differently from the dialog: set .Text and .Replacement.Text, then call .Execute with Replace:=wdReplaceAll. Always reset .Text and .Format = False between searches, because Find carries settings over from the previous call and this is the single most common source of surprising results.
  12. Export or save the result. To write a PDF, call ActiveDocument.ExportAsFixedFormat OutputFileName:=path, ExportFormat:=wdExportFormatPDF where path is a full file path string. To keep the original intact, save a copy with ActiveDocument.SaveAs2 to a new name rather than overwriting.
  13. Add error handling before you trust it. Wrap the body of the macro in On Error GoTo Failed with a label at the end that shows Err.Description, then use On Error GoTo 0 to restore normal behaviour. This turns a silent failure into a readable message.
  14. Save the document as macro-enabled. Use File > Save As and choose the Word Macro-Enabled Document format (.docm). A standard .docx silently drops the code when the document is closed.

Common Mistakes

  • Relying on Selection. Code that works while text is selected fails when nothing is selected. Move to Range objects.
  • Assuming zero-based collections. Word collections start at 1, unlike many other languages. Paragraphs(0) throws an error.
  • Forgetting to reset Find. Leftover format or wrap settings produce no matches or matches in the wrong place.
  • Using direct formatting when a style would do. Highlighting text manually means future style changes do not apply.
  • Saving as .docx. The macro disappears and the file opens with no code.
  • Editing the live document with no backup. One bad loop over paragraphs can strip formatting from the whole file.

Tips

  • Use the Object Browser (F2 in the editor) to browse every object, property and method Word exposes, including short descriptions.
  • The Macro Recorder (Developer > Record Macro) writes code for actions you perform, which is a fast way to discover the exact object names for a formatting task. Recorded code is verbose and Selection-heavy, so treat it as a reference to rewrite, not a finished product.
  • Name your variables meaningfully and keep each macro focused on one task; long procedures are hard to debug.
  • Word's object model overlaps with Excel's and PowerPoint's, but document-specific objects such as Range and Paragraphs do not exist in the same form elsewhere, so do not copy code between applications without checking.
  • Keep a scratch document full of headings, tables and lists so you can test loops safely.

Troubleshooting

If the macro does nothing at all, check that the cursor is inside the correct subroutine when you press F5 and that the module belongs to the open document's project. If you get a "Sub or Function not defined" compile error, look for a typo in an object or method name; the Object Browser will confirm the correct spelling. If macros are blocked entirely, Word's Trust Center macro settings are stopping them: open File > Options > Trust Center > Trust Center Settings > Macro Settings and choose an appropriate level, but only run code you wrote or trust. Documents downloaded from the internet may open in Protected View; enable editing before running anything.

If a loop runs far longer than expected, press Ctrl+Break to interrupt. A common cause is modifying a collection while iterating over it, for example deleting paragraphs inside a For Each loop. Iterate backwards with a numeric For i = Count To 1 Step -1 loop when deleting. If output appears in the wrong place, remember that Range positions shift as text is inserted, so set positions fresh after each edit.

Frequently Asked Questions

Does the VBA object model work in Word for the web or on mobile?

No. VBA editing and execution require the desktop application on Windows or Mac.

What is the difference between Range and Selection?

Selection is the user's current cursor or highlighted text and changes as the user clicks. Range is an independent span defined in code that does not move with the cursor, which makes macros predictable.

Can I record a macro and use that code as-is?

You can, but recorded macros rely heavily on Selection and record far more steps than needed. Use them to learn object names, then rewrite them around Range objects.

Why did my macro vanish after saving?

The document was saved in the standard .docx format, which cannot store macros. Save as .docm instead.

Where can I find the full list of objects?

Press F2 in the Visual Basic Editor to open the Object Browser, which lists every library, object, property and method available to Word.