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).

- Go to Developer > Code group > Macros > Create (or Alt+F11 to open the VBA editor directly).

- 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).

- In the code window, type a simple VBA macro, such as:
Sub InsertGreeting()Selection.TypeText Text:="Hello, " & Environ("USERNAME") & "!"Selection.TypeParagraphEnd Sub- This macro inserts a greeting with the user’s system username and a new paragraph.

- 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.

Step 3. Run a Custom Macro
Execute the Macro:
- Go to Developer > Macros, select the macro, and click Run.

- 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.

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.TypeParagraphSelection.TypeText Text:="Today is " & Format(Date, "mmmm d, yyyy") & "."Selection.TypeParagraphEnd Sub - Save changes in the VBA editor and test by running the macro.

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.

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.