To add VBA code to Word, open the Visual Basic Editor with Alt+F11, insert a module, paste your code, and save the document as a macro-enabled file (.docm). That is the whole process in brief. The rest of this guide walks through each step, explains where your code should live, and covers the mistakes that stop macros from running.

What You'll Need

  • A desktop version of Microsoft Word for Windows or Mac. The Visual Basic Editor is not available in Word for the web or in the mobile apps.
  • Macro security settings that allow macros to run. By default Word blocks macros from files downloaded from the internet, so you may need to unblock the file or trust its location.
  • Your VBA code, either written by you or copied from a trusted source. Only run code you understand or that comes from a reputable publisher.

Step-by-Step

  1. Enable the Developer tab (if it is not visible). Go to File > Options > Customize Ribbon. In the right-hand list, tick Developer and click OK. On Mac, go to Word > Preferences > Ribbon & Toolbar and tick Developer. Labels can differ slightly by version and region.
  2. Open the Visual Basic Editor. Press Alt+F11 (on Mac, Option+F11 or fn+Option+F11 depending on your keyboard). Alternatively, click Developer > Visual Basic. A separate editor window opens.
  3. Decide where the code should live. In the Project Explorer on the left, you will see your document and a Normal template. Code that should apply to every document goes in Normal. Code that belongs only to this file goes in the document's own project. If the Project Explorer is hidden, choose View > Project Explorer.
  4. Insert a module. With the correct project selected, choose Insert > Module. A blank code window appears. This is where standard macros belong. Use a document module (such as ThisDocument) only for event code that should run automatically when the document opens or closes.
  5. Paste or type your code. Copy your procedure into the module. Make sure you include the full Sub and End Sub lines. If you are typing, use the editor's indentation and let it flag syntax errors as you go.
  6. Check that it compiles. Choose Debug > Compile Project. If Word reports an error, it will highlight the offending line. Fix it before running anything.
  7. Run the macro to test it. Place the cursor inside the procedure and press F5, or choose Run > Run Sub/UserForm. You can also close the editor and use Developer > Macros, select the macro, and click Run.
  8. Save the file in a macro-enabled format. Go to File > Save As and choose Word Macro-Enabled Document (.docm) or Word Macro-Enabled Template (.dotm). A standard .docx file cannot store macros and will strip them on save.
  9. Handle the security prompt. When you reopen the file, Word may show a yellow message bar saying macros have been disabled. If you trust the file, click Enable Content. To avoid the prompt for your own files, put them in a trusted location via File > Options > Trust Center > Trust Center Settings > Trusted Locations.

Common Mistakes

  • Saving as .docx. This is the most frequent problem. Word warns you that macros will be lost, but if you click through, the code is gone. Always use .docm or .dotm.
  • Putting standard code in the wrong place. A normal macro pasted into the ThisDocument module will not appear in the macro list and may not run as expected. Use a standard module for ordinary procedures.
  • Leaving out Sub and End Sub. A fragment of code without the procedure wrapper will not compile.
  • Ignoring the message bar. Macros stay disabled until you click Enable Content, so the macro list may appear empty even though the code is there.
  • Copying code that references missing libraries. If a procedure uses an object from a library that is not referenced, you will get a compile error. Check Tools > References in the editor.
  • Forgetting that Normal.dotm carries code everywhere. Code saved in Normal runs in every document you open, which is convenient but risky if the code misbehaves.

Tips

  • Give each procedure a clear, unique name so it is easy to find in the Macros dialog.
  • Add a short comment at the top of each module describing what it does and any assumptions it makes.
  • Test macros on a copy of an important document before running them on the original, especially if the code deletes or reformats content.
  • Use the Immediate Window (View > Immediate Window) to test individual lines and inspect values while debugging.
  • Keep a backup of working code outside Word, in a text file or your own notes, so a corrupted template does not cost you the work.
  • If you share the file, tell recipients that it contains macros so they know why the security prompt appears.

Troubleshooting

The Developer tab is missing. Re-check the Customize Ribbon settings. On some managed workstations an administrator has hidden it; in that case Alt+F11 still opens the editor.

Alt+F11 does nothing. Another add-in may have captured the shortcut, or your keyboard requires the Fn key. Try Developer > Visual Basic from the ribbon instead.

The macro list is empty. Confirm the code is in a standard module, that it begins with Sub and ends with End Sub, and that macros are enabled. Also check that you are looking at the right document: the Macros dialog defaults to showing macros in all open documents and templates.

You get a compile error. Read the highlighted line. Common causes are a missing reference, a misspelled object name, or code pasted without its procedure wrapper. Use Debug > Compile Project after each fix.

The macro runs but does nothing. Step through it with F8 to see where it stops. Check that it is acting on the document you expect and that any selection or bookmark it relies on exists.

Word disables the macro every time. The file is probably marked as coming from the internet. Right-click the file in File Explorer, choose Properties, and tick Unblock if available. Alternatively, move the file to a trusted location. Only do this for files you trust.

You saved as .docx and the code vanished. The macros were stripped on save. Re-add the code and save immediately as .docm. There is no way to recover the lost code from the .docx file itself.

Frequently Asked Questions

Can I add VBA code in Word for the web or on mobile?

No. The Visual Basic Editor is only in the desktop versions of Word. Files with macros can be opened in Word for the web, but the macros will not run and you cannot edit them there.

What is the difference between a .docm and a .dotm file?

A .docm is a macro-enabled document you edit and share as a normal file. A .dotm is a macro-enabled template used as a starting point for new documents. Both can store VBA code; a plain .docx cannot.

Where should I put code that should run in every document?

In the Normal template's project. Open the editor, find Normal in the Project Explorer, insert a module there, and save. Be aware that this makes the code available in all documents and can affect performance or behaviour globally.

Why does Word show a security warning when I open my own file?

Word blocks macros by default, especially for files from the internet or email. Click Enable Content if you trust the file, or add its folder to Trusted Locations so the prompt stops appearing.

Can I edit a macro after it is saved?

Yes. Open the file, press Alt+F11, find the module in the Project Explorer, and edit the code directly. Save the file again in the same macro-enabled format.

Is it safe to run VBA code I found online?

Only if you trust the source and understand what the code does. VBA can read, modify, and delete files and can access other Office applications. Review the code before running it, and test it on a copy of your document.