简体   繁体   中英

Word VBA AutoNew macro not firing when opening Word

Using 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. They are working great except that neither one of them fires when I open Word from the taskbar or other icon. In that situation, Word opens with the blank "Document1," but without the initialization I want.

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

I tried AutoExec to cover this situation, but when AutoExec fires, there is no active window and no document loaded yet.

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. AutoNew does not fire when Word creates a new, default document when the application starts. (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. 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.

Sub AutoExec()

    Documents.Add

End Sub

In Word 2016 64bits AutoOpen is triggered before AutoExec. So if you Open a file, when AutoExec runs you can open a new file only if there is no file already opened. 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. So the solution cannot be applied. But you can ask Word to close the blank file in the AutoOpen macro.

Have you tried just adding code for the Document_New() and Document_Open() events in your Normal.dot file? I just tried it with mine and however I opened the test document I got the Document_Open() to fire. The Document_New() worked as well.

Screen shot of my setup for testing. The relevant msgbox includes the name of the document.

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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