Use a Word VBA Macro to Format Legal Documents
Question details
The user wants to automate repeated choices and optional paragraphs in a legal document using a Word macro.

- Product
- Microsoft Word
- Device & OS
- not provided
- Scenario
- Prepare legal documents from reusable boilerplate by prompting for information and including only applicable text.
- Observed behavior
- A macro fills repeated placeholders and retains or removes optional paragraphs according to the user's selections.
Open the document in a desktop version of Word that supports VBA, and save a working copy as a macro-enabled Word document (.docm). Identify the repeated fields and optional paragraphs, and review the wording with an appropriate legal professional.
Create a Word VBA macro for repeated fields and optional paragraphs
Use this approach when a document needs the same value in multiple places and a group of optional paragraphs should be included or removed together.
The example replaces every [PARTY_NAME] placeholder in the main document body and processes paragraphs beginning with [OPTIONAL]. All marked optional paragraphs are treated as one group. Apply the desired paragraph styles to the boilerplate before running the macro.
Keep the marked optional paragraphs outside tables, headers, and footers. Adapt the prompts and markers for additional choices, and review the completed document before use.
In the document body, type [PARTY_NAME] wherever the party name should appear. Start each optional paragraph with [OPTIONAL], for example: [OPTIONAL] This clause applies when selected.
In Word, open Developer > Visual Basic, then select Insert > Module. If Developer is hidden, enable it through Word's ribbon customization settings.
Paste this code into the module: Sub PrepareLegalDocument() Dim partyName As String Dim includeOptional As VbMsgBoxResult Dim i As Long Dim para As Paragraph Dim bodyRange As Range partyName = Trim$(InputBox("Enter the party name:")) If partyName = "" Then Exit Sub includeOptional = MsgBox("Include the optional paragraphs?", vbYesNoCancel + vbQuestion) If includeOptional = vbCancel Then Exit Sub Set bodyRange = ActiveDocument.StoryRanges(wdMainTextStory) With bodyRange.Find .ClearFormatting .Replacement.ClearFormatting .Execute FindText:="[PARTY_NAME]", ReplaceWith:=partyName, Replace:=wdReplaceAll End With For i = ActiveDocument.Paragraphs.Count To 1 Step -1 Set para = ActiveDocument.Paragraphs(i) If Left$(Trim$(para.Range.Text), 10) = "[OPTIONAL]" Then If Not para.Range.Information(wdWithInTable) Then If includeOptional = vbNo Then para.Range.Delete End If End If Next i If includeOptional = vbYes Then Set bodyRange = ActiveDocument.StoryRanges(wdMainTextStory) With bodyRange.Find .ClearFormatting .Replacement.ClearFormatting .Execute FindText:="[OPTIONAL]", ReplaceWith:="", Replace:=wdReplaceAll End With End If End Sub
Save the .docm file, then select Developer > Macros, choose PrepareLegalDocument, and click Run. Enter the party name, choose whether to include the optional paragraphs, and review the resulting text and formatting.

Edit legal document drafts with WPS Writer
WPS Office is a free, lightweight alternative for editing legal document drafts in a familiar interface. Its Microsoft Word format support helps you move existing documents into WPS Writer, though it does not fix Word VBA macro issues.

Frequently Asked Questions
Why should I save the document as a DOCM file?
Word's macro-enabled DOCM format can store VBA code. Use File > Save As and select Word Macro-Enabled Document before saving the macro with the document.
Can the macro fill the same name in several places?
Yes. Place [PARTY_NAME] at each occurrence in the main document body. The example uses Find and Replace to substitute the entered name throughout that body.
Can I choose optional paragraphs independently?
Not with the example as written: it includes or removes all [OPTIONAL] paragraphs together. Add separate markers and prompts in the VBA module for each independent choice.
Can I run this macro in Word for the web?
Word for the web does not run VBA macros. Open the document in a desktop version of Word that supports VBA, then use Developer > Macros.
Will the macro check whether a legal clause is appropriate?
No. The macro applies the choices you make; it does not provide legal advice. Review the selected clauses and final document with an appropriate legal professional.




