简体   繁体   English

打开 Word 时不触发 Word VBA AutoNew 宏

[英]Word VBA AutoNew macro not firing when opening Word

Using Word 2013.使用 Word 2013。

I found this previous question that is similar, but there was no accepted answer, and I'm not trying to build an add-in, so I hope I don't need something complex.我发现这个以前的问题是相似的,但没有被接受的答案,我不想构建一个加载项,所以我希望我不需要复杂的东西。

I have AutoNew and AutoOpen macros in my Normal.dotm.我的 Normal.dotm 中有 AutoNew 和 AutoOpen 宏。 They are working great except that neither one of them fires when I open Word from the taskbar or other icon.它们运行良好,只是当我从任务栏或其他图标打开 Word 时,它们都不会触发。 In that situation, Word opens with the blank "Document1," but without the initialization I want.在这种情况下,Word 会以空白的“Document1”打开,但没有我想要的初始化。

Suppose my macros display a message with the document's name when a document opens or is created.假设我的宏在打开或创建文档时显示带有文档名称的消息。 I know this is silly, but it seems like a workable example.我知道这很愚蠢,但这似乎是一个可行的例子。

If I open a document by clicking on it in File Explorer or by using File |如果我通过在文件资源管理器中单击文档或使用 File | 打开文档Open or File |打开或文件 | New, then the appropriate macro will fire and the message will pop up.新建,然后将触发相应的宏并弹出消息。

But if I just open Word from scratch, then no macro fires, and there is no message displaying "Document1."但是,如果我只是从头开始打开 ​​Word,则不会触发宏,也不会显示“Document1”消息。

I tried AutoExec to cover this situation, but when AutoExec fires, there is no active window and no document loaded yet.我尝试使用 AutoExec 来解决这种情况,但是当 AutoExec 触发时,没有活动窗口,也没有加载文档。

Ideas?想法?

The example cited in the question not "workable" because it makes no sense to want to show the name of an automatically generated document.问题中引用的示例不“可行”,因为想要显示自动生成的文档的名称是没有意义的。

In any case, only AutoExec will fire when the Word application is started - this is by design.在任何情况下,只有AutoExec会在 Word 应用程序启动时触发 - 这是设计使然。 AutoNew does not fire when Word creates a new, default document when the application starts.当 Word 在应用程序启动时创建新的默认文档时, AutoNew不会触发。 (You'll also notice if you immediately open another document, without doing anything to the default document, that document is closed automatically. In a manner of speaking, this default document doesn't exist until the user takes action on it.) (您还会注意到,如果您立即打开另一个文档,而不对默认文档进行任何操作,该文档将自动关闭。从某种意义上说,此默认文档不存在,直到用户对其进行操作。)

What can work is to explicitly create a new document during AutoExec.可以工作的是在 AutoExec 期间显式创建一个新文档。 This will have the effect of generating a document in place of the default document.这将具有生成文档代替默认文档的效果。 The document will have the same name as that default document.该文档将具有与该默认文档相同的名称。 The only different would be that this document will not close automatically when another document is opened.唯一不同的是,当打开另一个文档时,该文档不会自动关闭。

When the document is created then AutoNew (and Document_New ) will be triggered.创建Document_New将触发AutoNew (和Document_New )。

Sub AutoExec()

    Documents.Add

End Sub

In Word 2016 64bits AutoOpen is triggered before AutoExec.在 Word 2016 中,64 位 AutoOpen 在 AutoExec 之前触发。 So if you Open a file, when AutoExec runs you can open a new file only if there is no file already opened.因此,如果您打开一个文件,当 AutoExec 运行时,您只能在没有文件已打开的情况下打开一个新文件。 As a result when you open a file you don't have another blank document opened as well.因此,当您打开一个文件时,您不会同时打开另一个空白文档。

In Word 2010 and 2013 AutoExec is triggered before AutoOpen.在 Word 2010 和 2013 中,AutoExec 在 AutoOpen 之前触发。 So the solution cannot be applied.因此无法应用该解决方案。 But you can ask Word to close the blank file in the AutoOpen macro.但您可以要求 Word 关闭 AutoOpen 宏中的空白文件。

Have you tried just adding code for the Document_New() and Document_Open() events in your Normal.dot file?您是否尝试过在 Normal.dot 文件中为 Document_New() 和 Document_Open() 事件添加代码? I just tried it with mine and however I opened the test document I got the Document_Open() to fire.我只是用我的试了一下,但是我打开了测试文档,我让 Document_Open() 触发了。 The Document_New() worked as well. Document_New() 也有效。

Screen shot of my setup for testing.我的测试设置的屏幕截图。 The relevant msgbox includes the name of the document.相关的 msgbox 包括文档的名称。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM