Adding a text box via VBA in Microsoft Word lets you dynamically insert interactive input fields into documents, useful for forms, surveys, or automated data collection. This requires enabling Developer mode and writing a simple macro to create the control. No external tools or add-ins are needed—just Word’s built-in VBA editor.

What You'll Need

  • Microsoft Word 2010 or later (Windows or Mac)
  • Developer tab enabled in Word
  • Basic understanding of VBA syntax
  • A document where you want to insert the text box

Step-by-Step

  1. Open the Word document where you want to add the text box. Ensure the document is saved as a .docm (Macro-Enabled) file. If it’s not, go to File > Save As, choose Word Macro-Enabled Document (*.docm) from the file type dropdown, and save.
  2. Enable the Developer tab if it’s not visible. Go to File > Options > Customize Ribbon, check the box next to Developer under Main Tabs, then click OK.
  3. Press Alt + F11 to open the Visual Basic for Applications (VBA) editor. If the Project Explorer isn’t visible, press Ctrl + R to show it.
  4. In the Project Explorer, double-click ThisDocument under your document’s name (e.g., VBAProject (YourDocument.docm)). This opens the code window for the active document.
  5. Copy and paste the following VBA code into the code window:
Sub InsertTextBox()
    Dim tb As TextBox
    Set tb = ActiveDocument.InlineShapes.AddOLEControl(ClassType:="Forms.TextBox.1").OLEFormat.Object
    tb.Left = 100
    tb.Top = 100
    tb.Width = 200
    tb.Height = 25
    tb.Text = "Enter text here"
End Sub
  1. Close the VBA editor and return to Word. Press Alt + F8 to open the Macro dialog box.
  2. Select InsertTextBox from the list, then click Run. A text box will appear near the top-left corner of the document.
  3. Click inside the text box to type or edit text. The text box is now an active control that users can interact with when the document is opened.
  4. To modify the text box properties later, reopen the VBA editor and edit the code—adjust Left, Top, Width, Height, or Text values as needed. Re-run the macro to apply changes.
  5. Save the document again as a .docm file to preserve the macro and text box functionality.

Common Mistakes

  • Using a .docx file instead of .docm—this will delete the macro when saved, removing the text box.
  • Running the macro before enabling the Developer tab, which prevents access to the VBA editor.
  • Typing the code incorrectly—misplacing quotation marks, parentheses, or misspelling "Forms.TextBox.1" will cause runtime errors.
  • Expecting the text box to appear on every page—text boxes created this way are inline objects and only appear once, at the location specified.
  • Assuming the text box works in Word Online or on mobile—this feature only works in desktop Word applications.

Tips

  • To make the text box look cleaner, set its border to none by adding tb.BorderStyle = 0 after the height line in the code.
  • Use tb.Enabled = False to make the box read-only, or tb.Enabled = True to allow editing.
  • For multiple text boxes, duplicate the code block and change the variable name (e.g., tb2, tb3) and position values to avoid overlap.
  • Label the text box by inserting a static text field above it using Word’s normal text tools—VBA text boxes have no built-in labels.
  • Test the macro on a copy of your document first. If it crashes Word, disable macros when reopening and delete the problematic code.
  • Use the Immediate Window in VBA (Ctrl + G) to debug: type Debug.Print tb.Left to see current values during testing.

Troubleshooting

  • "Run-time error '430': Class does not support Automation or does not support expected interface"—this means the OLE class name is wrong. Use only "Forms.TextBox.1". Do not use "TextBox" alone or "Microsoft Forms TextBox 2.0"—those are outdated or incorrect.
  • Text box doesn’t appear after running the macro—check that the document is saved as .docm. Also, the text box might be positioned off-screen. Try changing Left and Top to 50 and 50 to bring it into view.
  • Macro is disabled when opening the document—enable macros by clicking "Enable Content" on the yellow security warning bar when you open the file. If you don’t see this bar, go to File > Options > Trust Center > Trust Center Settings > Macro Settings and select "Enable all macros" (only for trusted documents).
  • Text box disappears after saving and reopening—you likely saved as .docx. Reopen the .docm file and re-run the macro.
  • Word crashes when running the macro—close Word, restart it, then open the document in Safe Mode (hold Ctrl while opening Word). If it works then, a third-party add-in may be interfering. Disable add-ins one by one via File > Options > Add-Ins.

Frequently Asked Questions

Can I use a VBA text box in Word Online?

No. VBA macros and OLE controls like text boxes are not supported in Word Online, mobile apps, or any non-desktop version of Word. They only work in the Windows or Mac desktop applications.

Can I link the text box value to a cell in Excel?

Not directly. Word VBA cannot natively interact with Excel unless you write additional code to open Excel, create a workbook, and write the value—this requires advanced VBA and both apps running simultaneously.

How do I clear the text box content with a button?

Add another macro that sets the TextBox.Text property to an empty string: tb.Text = "". Then assign this macro to a button or shape in the document.

Can I make the text box accept only numbers?

Yes, but not with simple VBA. You’d need to use the TextBox’s Change event and validate input with code like On Error Resume Next and If Not IsNumeric(tb.Text) Then tb.Text = "". This requires event handling and is more complex.

Why does the text box move when I type?

This happens if the text box is set as a floating object. The code above creates an inline shape, which stays with text. If you manually drag it or change wrapping, it becomes floating. To fix, right-click the box, select Wrap Text > In Line with Text.

Is this text box secure for collecting sensitive data?

No. VBA text boxes in Word are not encrypted or secure. Do not use them for passwords, financial details, or personal identification. They are meant for simple, internal data collection only.