How to Create a Word VBA Macro That Inserts a Custom Field at Cursor
🔍 WiseChecker

How to Create a Word VBA Macro That Inserts a Custom Field at Cursor

You want to insert a custom field at the exact cursor position in a Word document without manually navigating menus every time. Word does not include a built-in command for inserting arbitrary field codes with one click. By writing a short VBA macro, you can insert any field code, such as DocProperty, UserName, or a custom formula, directly where your cursor sits. This article explains how to create, run, and assign a keyboard shortcut to a VBA macro that inserts a custom field at the insertion point.

Key Takeaways: Creating a VBA Macro to Insert a Custom Field

  • Alt+F11 (VBA Editor): Opens the environment where you write and store the macro code.
  • Selection.Fields.Add method: The VBA command that inserts a field at the current cursor position with a specific field code string.
  • File > Options > Customize Ribbon > Developer: Enables the Developer tab needed to access the Macros button and the Visual Basic Editor.

ADVERTISEMENT

What the VBA Macro Does and What You Need Before You Start

A VBA macro is a small program that automates repetitive tasks in Word. The macro you will create uses the Selection.Fields.Add method to insert a field at the exact location of the text cursor. A field is a set of codes that instructs Word to insert dynamic content, such as document properties, page numbers, or calculated values. The macro does not require any external add-ins; it runs entirely within Word’s VBA engine.

Before you create the macro, you need to enable the Developer tab in the Ribbon. The Developer tab provides access to the Visual Basic Editor (VBE), macro recording, and macro security settings. Without this tab, you cannot write or run macros. You also need to set your macro security level to allow all macros or at least to prompt before enabling them. The steps below assume you are using Word 2019, Word 2021, or Word for Microsoft 365 on Windows 10 or Windows 11. The same process works on Word 2016.

Steps to Create and Run a VBA Macro That Inserts a Custom Field at the Cursor

Enable the Developer Tab and Adjust Macro Security

  1. Open Word and go to File > Options
    Click File in the top-left corner, then click Options at the bottom of the left menu.
  2. Select Customize Ribbon
    In the Word Options dialog, click Customize Ribbon on the left sidebar.
  3. Check the Developer box in the right list
    Under Main Tabs, find Developer and check its box. Click OK to close the dialog. The Developer tab now appears in the Ribbon.
  4. Open the Macro Security settings
    Click the Developer tab, then click Macro Security in the Code group.
  5. Select Enable all macros
    In the Trust Center dialog, choose Enable all macros. Also check Trust access to the VBA project object model. Click OK. This setting allows your macro to run without interruption.

Write the VBA Macro Code

  1. Open the Visual Basic Editor
    On the Developer tab, click Visual Basic. Alternatively, press Alt+F11 on your keyboard.
  2. Insert a new module
    In the VBA Editor, click Insert on the menu bar, then select Module. A blank code window opens.
  3. Paste the macro code into the module
    Copy the following code and paste it into the white code area:

    Sub InsertCustomField()
    Dim fieldCode As String
    fieldCode = InputBox("Enter the field code to insert:")
    If fieldCode <> "" Then
    Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:=fieldCode
    End If
    End Sub

    This macro shows an input box where you type the field code, such as DOCPROPERTY "Author" or =SUM(1,2). If you enter a code, it inserts that field at the cursor.

  4. Save the macro
    Press Ctrl+S. In the Save As dialog, choose a name like “CustomFieldMacro” and keep the file type as .bas. The macro is now stored in the Normal.dotm template, making it available in all future documents.
  5. Close the VBA Editor
    Click the X in the top-right corner of the VBA Editor window to return to your document.

Run the Macro

  1. Place your cursor where you want the field
    Click in the document at the exact location where the field should appear.
  2. Open the Macros dialog
    On the Developer tab, click Macros. The Macros dialog lists all available macros.
  3. Select InsertCustomField and click Run
    Click the macro name, then click Run. An input box appears.
  4. Type the field code and press Enter
    For example, type DOCPROPERTY "Company" and press Enter. The field is inserted at the cursor position.

Assign a Keyboard Shortcut to the Macro (Optional)

  1. Go to File > Options > Customize Ribbon
    Click File, then Options, then Customize Ribbon.
  2. Click the Customize button next to Keyboard shortcuts
    At the bottom of the dialog, click Customize.
  3. Select Macros in the Categories list
    Scroll the left list and click Macros.
  4. Select InsertCustomField in the Commands list
    Click the macro name in the right list.
  5. Press a new shortcut key combination
    Click in the Press new shortcut key box and press, for example, Alt+Shift+F. Click Assign, then Close. Now you can press that shortcut to run the macro.

ADVERTISEMENT

Common Macro Issues and What to Watch For

The Macro Does Not Appear in the Macros List

If the macro is missing from the Macros dialog, you may have saved the module in a document that is not open. Macros stored in Normal.dotm are always available. To move a macro to Normal.dotm, open the VBA Editor, drag the module from the document’s project to the Normal project, or re-create the macro in a new module under Normal.

Word Shows a Security Warning and Blocks the Macro

If you see a yellow bar saying “Macros have been disabled,” your macro security is set to Disable all macros. Go to Developer > Macro Security and select Enable all macros. For better security, you can instead digitally sign the macro or store it in a trusted location.

The Field Does Not Update After Insertion

Fields inserted by the macro display the current value at the time of insertion. To update the field, right-click it and select Update Field, or press F9 with the field selected. To make the field update automatically when the document opens, include the \ MERGEFORMAT switch in the field code, for example: DOCPROPERTY "Author" \ MERGEFORMAT.

Inserting a Field That Requires a Bookmark or Named Range

Some field types, such as REF or PAGEREF, require a bookmark to exist in the document. If you insert a field referencing a missing bookmark, Word displays an error. Define the bookmark first by selecting text and pressing Ctrl+Shift+F5, then use the macro to insert the field with the correct bookmark name.

Manual Insertion vs VBA Macro: Field Insertion Methods Compared

Item Manual Insertion (Alt+F9 + Ctrl+F9) VBA Macro (Selection.Fields.Add)
Speed Multiple keystrokes per field One click or keyboard shortcut
Field code flexibility Must type code in brackets manually Accepts any valid field code string
Repeatability Repeat steps each time Macro can be run hundreds of times
Error risk Typo in brackets or code Code is consistent; input validation optional
Learning curve No VBA knowledge needed Basic VBA understanding required

You can now create a VBA macro that inserts a custom field at the cursor in Word. Start by enabling the Developer tab, writing the macro in the VBA Editor, and running it from the Macros dialog. For faster access, assign a keyboard shortcut like Alt+Shift+F. To extend the macro, modify the code to accept a preset field code without an input box, or to insert multiple fields in sequence.

ADVERTISEMENT