Custom Macros in MS Word

Last Updated : 7 Apr, 2026

Custom macros are user-created or modified VBA scripts in Microsoft Word that automate specific tasks. Unlike recorded macros, which simply capture user actions, custom macros are written directly in the VBA editor to perform more precise, complex, or conditional operations based on specific requirements.

Key Features

  • VBA Customization: Write scripts to automate tasks like inserting dynamic text or formatting based on conditions.
  • Flexible Logic: Include loops, conditionals, or user inputs not possible with recorded macros.
  • Reusable Code: Store macros in documents or templates for repeated use.

Example: Create a custom macro to insert a formatted date and user name into a document’s header.

Steps to Create and Use Custom Macros

Step 1. Access the VBA Editor

Open the VBA Editor:

  • Ensure the Developer tab is visible (go to File > Options > Customize Ribbon > check Developer).
Screenshot-2025-09-13-145417
  • Go to Developer > Code group > Macros > Create (or Alt+F11 to open the VBA editor directly).
Screenshot-2025-09-13-145517
  • In the VBA editor, the Project Explorer shows the document or template (e.g., Normal.dotm or ThisDocument).

Step 2. Create a Custom Macro

Write a New Macro:

  • In the VBA editor, click Insert > Module to create a new module (e.g., Module1).
Screenshot-2025-09-13-145818
  • In the code window, type a simple VBA macro, such as:
    Sub InsertGreeting()
    Selection.TypeText Text:="Hello, " & Environ("USERNAME") & "!"
    Selection.TypeParagraph
    End Sub
    • This macro inserts a greeting with the user’s system username and a new paragraph.
    Screenshot-2025-09-13-150346
  • Save the macro by clicking File > Save in the VBA editor.

Save the Document:

  • Save the document as a Word Macro-Enabled Document (*.docm) via File > Save As to preserve the macro.
Screenshot-2025-09-13-150513

Step 3. Run a Custom Macro

Execute the Macro:

  • Go to Developer > Macros, select the macro, and click Run.
Screenshot-2025-09-13-151150
  • Alternatively, assign the macro to a button or shortcut:
    • Go to Developer > Macros > select macro > Options.
    • Assign a Keyboard Shortcut (e.g., Ctrl+Shift+G) or add to the Quick Access Toolbar.

Apply to Document:

  • Place the cursor where the macro should execute (e.g., for inserting text) before running.
Screenshot-2025-09-13-151241

Step 4. Edit an Existing Macro

Modify VBA Code:

  • Go to Developer > Macros, select the macro, and click Edit to open the VBA editor.
  • Update the code, e.g., modify the “InsertGreeting” macro to include a date:
    Sub InsertGreeting()
    Selection.TypeText Text:="Hello, " & Environ("USERNAME") & "!"
    Selection.TypeParagraph
    Selection.TypeText Text:="Today is " & Format(Date, "mmmm d, yyyy") & "."
    Selection.TypeParagraph
    End Sub
  • Save changes in the VBA editor and test by running the macro.
Screenshot-2025-09-13-151355

Step 5. Manage Custom Macros

Organize Macros:

  • Use Developer > Macros > Organizer to copy macros between documents or templates.

Delete Macros:

  • In Developer > Macros, select the macro and click Delete to remove it.
Screenshot-2025-09-13-151455

Backup Macro-Enabled Files:

  • Save .docm files to OneDrive or a secure location to preserve custom macros.

Example: Copy a custom macro to Normal.dotm for use in all documents.

Comment