S
src
I've got a routine to check the file size of documents as they are
closed and suggest compressing pictures.
But I'm having a bear of a time trying to intercept the Close event.
Here's what I've got:
1) Macros are in a Global Addin (startup folder)
2) Need to intercept Close event of any file (not just those created
from the template)
3) Can't modify the Normal.dot.
I've tried the suggestions at
http://word.mvps.org/FAQs/MacrosVBA/PseudoAutoMacros.htm, and
http://word.mvps.org/faqs/macrosvba/AppClassEvents.htm
but to no avail.
I think I'm having trouble simulating the AutoOpen/AutoOpen command
from a global addin. When I use the following code in a template, I
can get it to work with documents based on that template, but not other
docs:
Sub AutoOpen()
Call Register_Event_Handler
End Sub
I've tried the following in a global addin so that I can work with any
doc, but I don't think it's working:
Private Sub oApp_NewDocument(ByVal Doc As Document)
' AutoOpen() macros can't be made global unless in Normal.dot. This is
a workaround.
Call Register_Event_Handler
End Sub
Anyone have any ideas?
THANKS
Here's my code thus far:
In a Module:
Option Explicit
Dim oAppClass As New EventClassModule
-------------
Sub Register_Event_Handler()
Set oAppClass.oApp = Word.Application
End Sub
-------------
Private Sub oApp_NewDocument(ByVal Doc As Document)
' AutoOpen() macros can't be made global unless in Normal.dot. This is
a workaround.
Call Register_Event_Handler
End Sub
-------------
In a Class Module named EventClassModule:
Public WithEvents oApp As Word.Application
---------------
Private Sub oApp_DocumentBeforeClose(ByVal Doc As Document, Cancel As
Boolean)
' Global Macro to intercept Close event and check file size
If Not ActiveDocument.Saved Then
Select Case MsgBox("Do you want me to save the changes to " & _
Split(ActiveDocument.Name, ".")(0) & "?", vbYesNoCancel +
vbExclamation, "save file")
Case vbYes
MsgBox "User opted to save. Saving and quitting"
ActiveDocument.Save
Call GetFileSize
Case vbNo
MsgBox "User opted not to save. Quitting without saving"
ActiveDocument.Saved = True
Case vbCancel
MsgBox "User opted to cancel. Cancelling quit"
Cancel = True
End Select
End If
End Sub
-----------------
closed and suggest compressing pictures.
But I'm having a bear of a time trying to intercept the Close event.
Here's what I've got:
1) Macros are in a Global Addin (startup folder)
2) Need to intercept Close event of any file (not just those created
from the template)
3) Can't modify the Normal.dot.
I've tried the suggestions at
http://word.mvps.org/FAQs/MacrosVBA/PseudoAutoMacros.htm, and
http://word.mvps.org/faqs/macrosvba/AppClassEvents.htm
but to no avail.
I think I'm having trouble simulating the AutoOpen/AutoOpen command
from a global addin. When I use the following code in a template, I
can get it to work with documents based on that template, but not other
docs:
Sub AutoOpen()
Call Register_Event_Handler
End Sub
I've tried the following in a global addin so that I can work with any
doc, but I don't think it's working:
Private Sub oApp_NewDocument(ByVal Doc As Document)
' AutoOpen() macros can't be made global unless in Normal.dot. This is
a workaround.
Call Register_Event_Handler
End Sub
Anyone have any ideas?
THANKS
Here's my code thus far:
In a Module:
Option Explicit
Dim oAppClass As New EventClassModule
-------------
Sub Register_Event_Handler()
Set oAppClass.oApp = Word.Application
End Sub
-------------
Private Sub oApp_NewDocument(ByVal Doc As Document)
' AutoOpen() macros can't be made global unless in Normal.dot. This is
a workaround.
Call Register_Event_Handler
End Sub
-------------
In a Class Module named EventClassModule:
Public WithEvents oApp As Word.Application
---------------
Private Sub oApp_DocumentBeforeClose(ByVal Doc As Document, Cancel As
Boolean)
' Global Macro to intercept Close event and check file size
If Not ActiveDocument.Saved Then
Select Case MsgBox("Do you want me to save the changes to " & _
Split(ActiveDocument.Name, ".")(0) & "?", vbYesNoCancel +
vbExclamation, "save file")
Case vbYes
MsgBox "User opted to save. Saving and quitting"
ActiveDocument.Save
Call GetFileSize
Case vbNo
MsgBox "User opted not to save. Quitting without saving"
ActiveDocument.Saved = True
Case vbCancel
MsgBox "User opted to cancel. Cancelling quit"
Cancel = True
End Select
End If
End Sub
-----------------