We help IT Professionals succeed at work.

word macro in normal.dotm run when specific file opened

etech0
etech0 asked
on
Is there a way to create a macro in my "Normal" file that will run whenever a specific file is opened?
For security reasons I cannot have the macro in that file, as it's on the network.
Thanks!
Comment
Watch Question

CERTIFIED EXPERT

Commented:
opened by yourself or anyone? if you are looking at this from a security logging point of view then Word really isnt up to the job.

If its to track when its opened in your copy of word and it doesn't use a different template then you could identify it as long as the name doesn't change.

What are you actually trying to achieve

Reg

Author

Commented:
Just me.
Basically, we have a specific file on the network that many people use.
What I want is that every time I open it (on my computer only) a specific macro will run in the file. However, I cannot store macros in the file for security reasons.
Can you help me?
CERTIFIED EXPERT

Commented:
etecho,

Pretty much the same as Excel. First in an ordinary module in your Normal...
Option Explicit

Public AppClass As New EventClass

Sub AutoOpen()
Set AppClass.App = Application
MsgBox "App_DocumentOpen Started."

End Sub

Open in new window

Then, also in your Normal, in a Class module named EventClass...
Option Explicit

Public WithEvents App As Application

Private Sub App_DocumentOpen(ByVal Wb As Word.Document)
    MsgBox "Opening..." & vbTab & Wb.Name
End Sub

Open in new window

Regards,
Brian.

Author

Commented:
When does the code in the Class Module run, and when does the code in the regular module run?
CERTIFIED EXPERT

Commented:
etech0,

Excellent point! Forget about the whole Class rigmarole, you just need the following...
Sub AutoOpen()

MsgBox ActiveDocument.Name & " Opened."

End Sub

Open in new window

Regards,
Brian.

Author

Commented:
Where should I put it?
CERTIFIED EXPERT

Commented:
etech0,

In any ordinary (i.e. not Class) module in your Normal.Dotm. Do you already have macros in it?

Oh, you may already have an AutoOpen, if so you just need to add the MsgBox line to it. (BTW, Word has a second way of handling AutoOpen - do you have a module (i.e. not a macro) called AutoOpen?

Regards,
Brian.

Author

Commented:
I have a module called autoopen, containing the following:

Option Explicit

Public AppClass As New EventClass

Sub AutoOpen()
Set AppClass.App = Application
MsgBox "App_DocumentOpen Started."

End Sub


what's the difference between the two ways of handling autoopen?
GrahamSkanRetired
CERTIFIED EXPERT
Top Expert 2012

Commented:
You can also use the Document_New event in the ThisDocument module of the Normal template
CERTIFIED EXPERT

Commented:
etecho,

From the Word 2010 Help...
Word Developer Reference
Auto Macros

By giving a macro a special name, you can run it automatically when you perform an operation such as starting Microsoft Office Word or opening a document. Word recognizes the following names as automatic macros, or "auto" macros.

Macro name                    When it runs
------------                     ------------------------------------------
AutoOpen                        Each time you open an existing document

Auto macros in code modules are recognized if either of the following conditions are true.
 - The module is named after the auto macro (for example, AutoExec) and it contains a procedure named "Main."
 - A procedure in any module is named after the auto macro.


Add a MsgBox to the existing one so you check that it runs.

Regards,
Brian.
CERTIFIED EXPERT

Commented:
etech0,

Oops, you already have one there. If it's not running then change the macro name to "main" and see if that works.

Regards,
Brian.
CERTIFIED EXPERT

Commented:
etecho,

Your AutoOpen module looks suspiciously like the code I gave you originally! If so, just delete the whole module (assuming there;s nothing else in it and you're not using "AppClass" elsewhere.

If you're not sure, just post your Normal.dotm here.  :)

Regards,
Brian.

Author

Commented:
I deleted autoopen, and I have this in my EventClass class module:
However, it does not seem to be running!
Option Explicit

Public WithEvents App As Application

Private Sub App_DocumentOpen(ByVal Wb As Word.Document)

msgbox("Code is running")

End Sub

Open in new window

CERTIFIED EXPERT
Commented:
etch0,

Apologies, that was my initial irrelevancy. Delete the whole module (again, assuming nothing else in it).  All you need is the following in an ordinary module...
Sub AutoOpen()

MsgBox ActiveDocument.Name & " Opened."

End Sub

Open in new window

Probably best to just upload your Normal.Dotm!

Regards,
Brian.

Author

Commented:
YAY!!!!

Thanks for all your help!
CERTIFIED EXPERT

Commented:
Thanks kindly, etecho! Apologies for the over-elaboration!

Explore More ContentExplore courses, solutions, and other research materials related to this topic.