4.4Macros & VBA
The essentials in 30 seconds
- Macros record sequences of actions to automatically replay them and save time.
- The .docm format is mandatory for macros; .docx disables them for security reasons.
- The VBA editor (Alt+F11) allows you to view, modify and create advanced macros.
Fil Imane — Macros & VBA
Imane repeats the same shaping operations every week. It records a simple macro, discovers the VBA, and verifies that the macro remains available in the correct file format.
Continuation of the common thread: a logistics SME in Algiers professionalizes its documents.
1. What is a macro?
A macro is a recording of a sequence of actions that can be replayed with one click. Concretely, Word records each action you perform (formatting, insertions, navigations) and replays them automatically.
Macros are particularly useful for repetitive tasks: applying specific formatting, inserting a standard header, cleaning up an imported document, creating a table of contents. A 30-second macro can replace 10 minutes of manual work.
A macro is saved in a document in .docm format (Word document that supports macros) or in the global template normal.dotm. The .docx format does not support macros for security reasons.
2. Record a macro
To save: go to Developer → Save Macro (or click the button in the status bar at the bottom). Give a name (no spaces, no special characters), choose an optional keyboard shortcut, and select storage (this document or normal.dotm).
Once recording is started, all your actions are recorded: typed text, formatting, navigation, insertion. Click Stop Recording as soon as the sequence is finished.
⚠️ Attention: the recorder records everything, including errors. If you make a mistake, undo it (Ctrl+Z) before stopping, or delete the macro in the VBA editor to correct. Remember to mentally plan your sequence before you start recording.
3. Basics of VBA for Word
VBA (Visual Basic for Applications) is the programming language for Office macros. The VBA editor opens with Alt+F11. This is where you can view, edit and create macros.
A basic VBA macro looks like:
Sub MacroExample()
Selection.Style = ActiveDocument.Styles("Heading 1")
Selection.TypeText Text:="New paragraph"
Selection.TypeParagraph
End Sub
Key concepts: Sub (routine without return value), Function (with return), Dim (variable declaration), If…Then (condition), For…Next (loop). The VBA uses the Word object (ActiveDocument, Selection) to interact with the document.
4. Security and macro management
Macros are a common malware vector. Word applies a multi-level security system: by default, macros are disabled in documents downloaded or received by email.
You can set the security level in File → Options → Trust Center → Macro Settings. Options include: disable all macros, enable signed macros only, or enable all macros (not recommended).
To share your macros securely, use digital certificates to sign your VBA projects. Macros signed by a trusted certificate will be executed without warning. Keep .docm files on reliable media and inform your users before running a macro.
🔎 Concrete example — Worked Example — Quick Style Application Macro
// Before: document with unformatted text selected
// Recorded macro:
Sub ApplyQuickStyle()
' Applies 'Quote' style to the selection
Selection.Style = ActiveDocument.Styles("Quote")
' Adds a blank line after
Selection.TypeParagraph
End Sub
// Shortcut: Ctrl+Shift+Q
// Usage: select text → Ctrl+Shift+Q
Record a macro that applies a custom style in a shortcut.
Name your macros without spaces or special characters (use PascalCase)
Custom keyboard shortcut enables lightning-fast execution
The VBA editor (Alt+F11) allows you to modify the macro after saving
Always save as .docm, never as .docx, to preserve macros
Section vocabulary
- Word Macro
- Record a sequence of repetitive actions that can be automatically replayed to automate common tasks
Simple example: Imane applies “Macro Word” in her professional documents.
What it is not: Treat “Word Macro” as a detail with no impact on the document.
Reflex: Does this choice make the document clearer and more reliable?
- VBA
- Programming language (Visual Basic for Applications) used to create and modify Microsoft Office macros
Simple example: Imane applies “VBA” in her professional documents.
What it is not: treat “VBA” as a detail with no impact on the document.
Reflex: Does this choice make the document clearer and more reliable?
- .docm
- Word file format that supports macros (as opposed to .docx which disables them for security)
Simple example: Imane uses “.docm” in her professional documents.
What it is not: treat “.docm” as a detail with no impact on the document.
Reflex: Does this choice make the document clearer and more reliable?
- VBA editor
- Integrated development environment (Alt+F11) to view, modify and create VBA macros and procedures
Simple example: Imane applies “VBA Editor” in her professional documents.
What it is not: Treat “VBA Editor” as a detail with no impact on the document.
Reflex: Does this choice make the document clearer and more reliable?
In practice — Create a standard header insert macro
- Open a document and go to Developer → Record macro
- Name the macro “SaveStandardHead” and choose a shortcut
- Go to Insert → Header → Edit Header
- Enter the header text and apply the desired formatting
- Close header and stop recording
- Test the macro via the shortcut in a new document
- Open the VBA editor (Alt+F11) to inspect the generated code
Script ready to say — Macros & VBA (25 s)
“Record and run macros, understand VBA basics, manage macro security, and create simple automations to save time. Concretely, I check this point before distributing the document: the macros record sequences of actions to replay them automatically and save time. »
Short wording, to be adapted to the recipient and the context of the document.
Copy and paste template — “Macros & VBA” checklist
CHECKLIST — Macros & VBA
1. Macros record sequences of actions to automatically replay them and save time.
2. The .docm format is mandatory for macros; .docx disables them for security reasons.
3. The VBA editor (Alt+F11) allows you to view, modify and create advanced macros.
4. Macro security is managed by the Trust Center with different activation levels.
EXPECTED RESULT: A one-click macro that automatically inserts a standardized header into any document.
Express version
Apply only the essentials and the first key point, then check the rendering in a short document.
Remote
Share a PDF and check fonts, margins and pagination before sending.
As a team
Lock in the common model and document choices to keep colleagues consistent.
Printed version
Check pagination, headers and contrast before printing.
✓ Best practices
- Plan your sequence before you start recording: a well-planned macro is an error-free macro.
- Use custom keyboard shortcuts for commonly used macros (avoid existing combinations like Ctrl+C).
- Learn the basics of VBA (Alt+F11) to modify your macros and make them more robust with conditions and loops.
- Sign your macros with a certificate to avoid security alerts for recipients.
✗ Common pitfalls
- Save in .docx format: macros are deleted when saved, with no visible error.
- Forgetting to stop recording: all subsequent actions are recorded unnecessarily.
- Macro registered in normal.dotm without knowing it: the macro runs in all documents, not just the intended one.
- Enabling macros without verification: security risk if the document comes from an unknown source.
Key takeaways
- Macros record sequences of actions to automatically replay them and save time.
- The .docm format is mandatory for macros; .docx disables them for security reasons.
- The VBA editor (Alt+F11) allows you to view, modify and create advanced macros.
- Macro security is managed by the Trust Center with varying levels of activation.
48 hour challenge — apply tomorrow
Resume creating a standard header insertion macro on a real Imane PME document: Open a document and go to Developer → Save macro. Time it for 20 minutes and note what has changed.
- I can explain and apply: Macros record sequences of actions to replay them automatically and save time.
- I can explain and apply: The .docm format is mandatory for macros; .docx disables them for security reasons.
- I can explain and apply: The VBA editor (Alt+F11) allows you to view, modify and create advanced macros.
Frequently asked questions
Can you password protect a macro?
Yes: in the VBA editor, tools → VBAProject Properties → Protection → Lock project for viewing.
Can a macro interact with multiple documents?
Yes, the VBA can manipulate all documents opened via ActiveDocument or Documents("name"). This is useful for batch processing.
How to share a macro without risk?
Sign it with a digital certificate, or export the VBA module via the editor and import it into the recipient's document.
Does Word Online support macros?
No, Word Online does not run VBA macros. Macros require Word desktop (Windows or Mac).
In what Word file format are macros stored?