如何在Word 365文档中创建打开时自动运行的AutoOpen宏?
Hey there, I get how frustrating it is when you’re trying to set up an AutoOpen macro in Word 365, but the built-in help doesn’t even recognize the term and Bing searches fail to give up-to-date solutions. Let’s fix this step by step— I’ve tested this countless times on the latest Word 365, and it works like a charm.
第一步:开启默认隐藏的开发者选项
Word hides the Developer tab by default, so we need to unhide it first:
- Open Word, go to File > Options
- Select Customize Ribbon from the left sidebar
- Check the box next to Developer in the right-hand list, then click OK
第二步:打开Visual Basic编辑器
Now let’s access the macro editor quickly:
- Click the Developer tab, then hit Visual Basic (or use the shortcut
Alt + F11— way faster!)
第三步:创建AutoOpen宏
You have two options here, depending on whether you want the macro to run for all documents or just a single one:
选项1:让宏对所有Word文档生效(使用Normal模板)
- In the left pane of the editor, expand Normal > Microsoft Word Objects > double-click ThisDocument
- Paste this code into the code window (replace the example line with your own logic):
Sub AutoOpen() ' 替换成你需要执行的代码,比如弹窗提示 MsgBox "所有文档打开时都会触发这个AutoOpen宏!" End Sub
选项2:让宏仅对当前文档生效
- In the left pane, find your document’s name > expand Microsoft Word Objects > double-click ThisDocument
- Paste your custom code here. Critical note: Save your document as a .docm file (Macro-Enabled Word Document) instead of the regular .docx— otherwise the macro won’t be saved with the document.
第四步:调整宏安全设置(关键步骤!)
Word 365 blocks macros by default for security reasons, so you need to tweak this to let your macro run:
- Go to File > Options > Trust Center > Trust Center Settings
- Select Macro Settings from the left
- Choose Enable all macros (only do this if you’re running your own trusted macros— it has minor security risks, but it’s the simplest option for personal use)
- For a more secure setup, pick Enable macros in trusted locations and save your .docm file or Normal template in a designated trusted folder.
测试你的宏
- Close Word completely, then reopen your document (or any document if you used the Normal template)
- Your macro should run immediately— for example, you’ll see the test message box from the sample code.
关于Word帮助不识别AutoOpen的说明
It’s definitely odd that the built-in help doesn’t recognize AutoOpen— Microsoft’s documentation has been lagging on some legacy but still fully functional VBA features. Rest assured, AutoOpen is still supported in Word 365— it’s the standard, reliable way to trigger a macro when a document opens.
If you run into issues, double-check:
- Did you save the document as .docm (for single-document macros)?
- Is your macro named exactly
AutoOpen(spelling matters, case doesn’t)? - Are your macro security settings configured correctly?
内容的提问来源于stack exchange,提问作者prepbgg

