Steve Cross
Steve Cross

Reputation: 99

How do I run a macro on word when I open a specific filename

I have a document (.doc) that is sent to me daily. I want to run a macro when this document is opened but only on this document, not others. It has the same name every time. How can I detect if this is the document that was opened before I run the macro. Thanks!

Upvotes: 0

Views: 864

Answers (1)

Rich Michaels
Rich Michaels

Reputation: 1713

Put the following code into a Module in your Normal template. Modify it to test for the name of the document you open everyday.

As long as Word is already open, to even a blank unnamed document, this routine will run when you open, from Word, the daily document. You can add to this routine a call to your custom macro.

Sub AutoOpen()
    If ActiveDocument.Name = "MyTest.docx" Then
        MsgBox "Hi I've Been Opened"
    End If
End Sub

Upvotes: 1

Related Questions