To insert text, a paragraph, or a field into a Word document with VBA, you use the Insert method on a Range object. The shortest working pattern is: set a range, then call InsertAfter or InsertBefore on it, or use Range.InsertAfter "your text" directly. For example, ActiveDocument.Range.InsertAfter "Hello" appends text at the end of the document. The Insert method itself is used for inserting a file, a paragraph mark, or a field, depending on the arguments you pass. This guide walks through the practical patterns, the arguments that matter, and the errors that trip people up.
What You'll Need
- Microsoft Word on Windows or Mac (the VBA editor is built in; no add-in required).
- A document open in Word, or a macro-enabled template (.dotm) if you want the code available to new documents.
- Basic familiarity with opening the VBA editor: press Alt+F11 on Windows, or use Tools > Macro > Visual Basic Editor on Mac. Menu labels can differ slightly by Word version and platform.
- A macro-enabled document (.docm) if you plan to save the code with the file.
Step-by-Step
- Open the VBA editor. In Word, press Alt+F11 (Windows) or go to Tools > Macro > Visual Basic Editor (Mac). The editor opens in a separate window.
- Insert a module. In the Project Explorer, right-click your document or template, choose Insert > Module. A blank code window appears. Code in a standard module is available to all macros in that document.
- Write a simple insertion macro. Type the following, replacing the text with your own:
This usesSub InsertTextAtEnd() Dim doc As Document Set doc = ActiveDocument doc.Range.InsertAfter "Inserted at the end." End SubInsertAfteron the whole-document range, which places text after the final character.InsertBeforedoes the same at the start. - Insert at a specific location. Use a
Rangeobject that points to the spot you want. For example, to insert after the first paragraph:
The inserted text inherits the formatting of the range it is inserted into, which is usually what you want.Sub InsertAfterFirstParagraph() Dim rng As Range Set rng = ActiveDocument.Paragraphs(1).Range rng.InsertAfter " New sentence." End Sub - Use the Insert method for files and fields. The
Range.Insertmethod takes arguments such asFileName,Range,ConfirmConversions,Link,Attachment, andText. For example, to insert the contents of a text file:
Adjust the path to a file that exists on your machine. If the file is not found, Word raises a run-time error.Sub InsertFileContents() Dim rng As Range Set rng = Selection.Range rng.Insert FileName:="C:\\Temp\\notes.txt", ConfirmConversions:=False End Sub - Insert a paragraph mark. To add a new paragraph without typing text, use
InsertParagraphAfteron a range, or insert the paragraph mark character withInsertAfter vbCr. The constantvbCris a carriage return; in Word VBA it creates a new paragraph. - Insert a field. Use
Fields.Addon a range rather thanInsertfor fields. For example,ActiveDocument.Range.Fields.Add Range:=Selection.Range, Type:=wdFieldDateinserts a date field at the cursor. TheInsertmethod is not the field-insertion method; mixing them up is a common source of confusion. - Run the macro. Press F5 while the cursor is inside the subroutine, or go back to Word and use View > Macros (Alt+F8 on Windows) to select and run it. If the macro does not appear, check that you saved the code in the correct document or template and that macros are enabled.
- Save the document as .docm. If you want the macro to travel with the file, use File > Save As and choose Word Macro-Enabled Document (*.docm). Saving as .docx removes the VBA project.
Common Mistakes
- Using
Insertfor plain text.Range.Insertis for files, not arbitrary strings. For text, useInsertAfterorInsertBefore, or assign toRange.Text. - Forgetting to set the range. Calling
InsertAfteron an uninitializedRangeobject raises error 91 (Object variable or With block variable not set). Always useSet rng = ...first. - Assuming the cursor moves. Inserting text through a range does not move the selection unless you explicitly collapse or select the range afterward. If you need the cursor after the inserted text, set
Selection.Collapse wdCollapseEndor useSelection.TypeTextinstead. - Hard-coding file paths. A path that works on your machine fails on another. Use
ThisDocument.Pathor prompt the user with a file dialog if the macro is shared. - Inserting into a protected document. If the document is protected, insertion fails unless the range is in an editable region. Check
ActiveDocument.ProtectionTypebefore inserting.
Tips
- Use
Withblocks to keep range code readable:With ActiveDocument.Range .InsertAfter "A" .InsertAfter "B" End With. Each call appends after the previous insertion because the range expands. - To insert formatted text, insert plain text first, then apply formatting to the returned range.
InsertAfterdoes not return a range, so capture the start and end positions before and after insertion if you need to format only the new text. - For repeated insertions, build a string and insert once. Many small insertions are slower and can trigger screen repaints; set
Application.ScreenUpdating = Falseat the start and restore it at the end. - Test on a copy of the document. Insertion macros are easy to run twice, and undo history may not cover every operation in every version.
Troubleshooting
If the macro does nothing, first check that macros are enabled: File > Options > Trust Center > Trust Center Settings > Macro Settings (labels vary by version). Next, confirm the code is in a standard module, not a document module tied to a specific event. If you get a compile error, look for a missing Set or a misspelled method name; VBA highlights the offending line. If insertion lands in the wrong place, print the range's start and end positions with Debug.Print rng.Start; rng.End before inserting. If Word raises a file-not-found error from Insert, verify the path and that the file is not open in another program. For protected documents, unprotect only if you have the password and are authorized to edit; otherwise insert into an unprotected section. If the document becomes corrupted after a failed insertion, close without saving and reopen a backup.
Frequently Asked Questions
What is the difference between Range.Insert and Range.InsertAfter?
InsertAfter adds text at the end of the range. Insert inserts a file or other object at the range, using arguments such as FileName. For plain text, use InsertAfter or assign to Range.Text.
Can I insert text at the cursor with a range?
Yes. Use Selection.Range.InsertAfter "text" or Selection.TypeText "text". The first keeps the selection; the second types as if from the keyboard.
Why does my inserted text lose formatting?
Inserted text inherits formatting from the range it enters. If the range spans mixed formatting, Word may apply the formatting of the first character. Insert into a consistently formatted range, or format the new text afterward.
Does Range.Insert work in Word for Mac?
The VBA object model is largely the same, but some file-path and dialog behaviors differ. Use forward slashes or the Mac path format, and test the macro on the target machine.
How do I insert a line break instead of a new paragraph?
Use InsertAfter vbLf for a manual line break, or InsertAfter vbCr for a new paragraph. The two behave differently in Word's paragraph model.
Can I undo an insertion made by VBA?
Usually yes, with Ctrl+Z, but not every VBA operation is recorded in the undo stack. Test on a copy and keep a backup before running bulk insertion macros.