etech0
asked on
word macro in normal.dotm run when specific file opened
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!
For security reasons I cannot have the macro in that file, as it's on the network.
Thanks!
ASKER
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?
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?
etecho,
Pretty much the same as Excel. First in an ordinary module in your Normal...
Brian.
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
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
Regards,Brian.
ASKER
When does the code in the Class Module run, and when does the code in the regular module run?
etech0,
Excellent point! Forget about the whole Class rigmarole, you just need the following...
Brian.
Excellent point! Forget about the whole Class rigmarole, you just need the following...
Sub AutoOpen()
MsgBox ActiveDocument.Name & " Opened."
End Sub
Regards,Brian.
ASKER
Where should I put it?
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.
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.
ASKER
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?
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?
You can also use the Document_New event in the ThisDocument module of the Normal template
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.
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.
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.
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.
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.
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.
ASKER
I deleted autoopen, and I have this in my EventClass class module:
However, it does not seem to be running!
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
YAY!!!!
Thanks for all your help!
Thanks for all your help!
Thanks kindly, etecho! Apologies for the over-elaboration!
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