PDA

View Full Version : macro to fire for any document open



WallIT
08-23-2011, 06:11 AM
Hi,

I think what I'm trying to achieve is fairly easy. I want a macro to run whenever any document is opened.

I have read the MVPS article titled "How to create global event procedures similar to AutoOpen, AutoNew and AutoClose, without using Normal.dot" (if you google that, you'll find the article (I can't paste links in this topic as I don't have a high enough post count)). However, I'm struggling to implement it.

I already have a global.dot which is deployed on my network and everyone's Word File Locations points at this at startup.

So, my questions are:

1. Can I add the necessary code to the global.dot template and will this fire the macro whenever Word opens an existing document?

If so, then...

2. The code: from that link above, I can't work out where (for Word 2003) I should be inserting the code. Should it be a module? or class module? or in the 'ThisDocument' object? I have tried putting it in all three places but it doesn't fire.

For the sake of clarification, the code I am trying to fire is

Private Sub oApp_NewDocument(ByVal Doc As Document)
ThisDocument.RemovePersonalInformation = False
End Sub

I'm sure some clever people out there can spot my mistake...??

Thanks very much

Frosty
08-23-2011, 10:26 AM
You're close. A quick concept lesson, and then the code:
1. ThisDocument always refers to the current code project. Since you want this code to fire when any document is opened (I assume) and operate on that document, I'm not sure your ThisDocument.RemovePersonalInformation = False is correct. However, that's a separate issue. The below code will simply fire whenever a document is opened, show a messagebox with the document name. Then you'll have to ask a further question (if you need) on what you need to actually happen.

In a regular code module, you need to add the following:

Option Explicit
Private m_appWord As ThisApplication
'----------------------------------------------------------------------------------------------
'This fires when the global addin is loaded...
'if the global addin is in the word startup, it essentially fires when Word starts
'----------------------------------------------------------------------------------------------
Public Sub AutoExec
Event_Handler_Register
End Sub
'----------------------------------------------------------------------------------------------
'Register the event handler for document events
'----------------------------------------------------------------------------------------------
Public Sub Event_Handler_Register()
'don't create a new object if it's already in memory
If m_AppWord Is Nothing Then
Set m_AppWord = New ThisApplication
End If
End Sub
'----------------------------------------------------------------------------------------------
'Unregister the event handler for document events
'----------------------------------------------------------------------------------------------
Public Sub Event_Handler_Unregister()
On Error Resume Next
Set m_AppWord = Nothing
End Sub

Then you also need to create a new class module, call it "ThisApplication" (it can be called anything, this is just my naming convention)
That code module should contain the following:

Option Explicit
Private WithEvents myApp As Word.Application
'----------------------------------------------------------------------------------------------
'When the class is instantiated, automatically synchronize to the Application
'----------------------------------------------------------------------------------------------
Private Sub Class_Initialize
Set myApp = Word.Application
End Sub
'----------------------------------------------------------------------------------------------
' This event fires when a document is opened by the application.
' Look at the drop down for other available events (varies by version)
'----------------------------------------------------------------------------------------------
Private Sub myApp_DocumentOpen(ByVal doc As Document)
Msgbox "Just opened: " & doc.FullName
End Sub

There are a lot of ways to structure this (obviously, you don't necessarily need the ability to unregister your event handler, but it's good programming practice to always create functions in "pairs" (creation and deletion of things).
Also, you will find that application events can fail some times, depending on what other bugs happen (i.e., if you don't error trap and show the microsoft debug window, and your user hits "End" to get out of some buggy routine, your event handler is likely to go poof as well-- so it's useful to have that code simply called by the AutoExec routine, and then you can call it at other times as well).

Hope this helps.

EDIT: depending on how extensive your existing global addin is, you may need to find an existing AutoExec, rather than have two. But this is simply for a proof of concept.