How to intercept Close Event from Global Addin?

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
-----------------
 
S

src

OK, so now I'm getting the strange behaviour that I'm being asked if I
want to save twice.

Here's what I've got:

in module EventHandlers:
-----
Public wehEventSink As New WordEventHandler
-----
Public Sub AutoExec()
' Register the Event Handler
Set wehEventSink.WordApp = Word.Application
End Sub
-----

in module class WordEventHandler:
-----
Public WithEvents WordApp As Word.Application
-----
Private Sub WordApp_DocumentOpen(ByVal docOpened As Word.Document)
MsgBox "WordApp_DocumentOpen event handler"
End Sub ' WordApp_DocumentOpen
-----
Private Sub WordApp_DocumentBeforeClose(ByVal docClosing As
Word.Document, ByRef Cancel As Boolean)
MsgBox "WordApp_DocumentBeforeClose event handler"
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
-----
Private Sub WordApp_Quit()
MsgBox "WordApp_Quit"
End Sub
-----

in module FileSize:
-----
Sub GetFileSize()
MsgBox ("GetFileSize()")
Dim OpenFileSize As Long
Dim OpenNumPages As Long
Dim NewFileSize As Long
' Get Filesize of saved document
OpenFileSize = ActiveDocument.BuiltInDocumentProperties("Number of
Bytes")
' Get Number of pages of document
OpenNumPages = ActiveDocument.BuiltInDocumentProperties("Number of
Pages")
' Compare
If OpenFileSize / OpenNumPages > 30000 Then
Dim intMsgBoxResult As Integer
intMsgBoxResult = MsgBox("The file is only " & OpenNumPages & "
pages and currently over " & FormatNumber(OpenFileSize / 1048576, 2) &
_
" MB. Would you like to compress images?", vbYesNo +
vbQuestion, "Bloated File Size")

If intMsgBoxResult = vbYes Then
'Do something
Call CompressPics
ActiveDocument.Save
NewFileSize = ActiveDocument.BuiltInDocumentProperties("Number
of Bytes")
MsgBox ("The new file size is " & FormatNumber(NewFileSize /
1048576, 2) & " Mb. " & vbCr & vbCr & "That's " &
FormatNumber((NewFileSize / OpenFileSize) * 100, 0) & "% of the
original.")
End If
End If
End Sub
-----


When I make changes then quit the file, I get the message boxes
indicating the sequence:
DocumentBeforeClose
"Want Me to Save Changes?..."
Y
Saving and Quitting
GetFileSize
"Want Me to Save Changes?..." <- here's the second one
Y
closes

It appears that the GetFileSize routine touches the file (probably the
OpenFileSize and OpenNumPages), and changes the Save status. So,
before the file *actually* closes, it brings up the custom FileSave
dialog again.

Any ideas?

Thanks!
 
S

src

Check that - the second save dialog box is the MS Word dialog. Does
anyone know why the regular save dialog is triggering even after my
custom one has saved the file?

Should be:
When I make changes then quit the file, I get the message boxes
indicating the sequence:
DocumentBeforeClose
"Want Me to Save Changes?..." <- my custom dialog
Y
Saving and Quitting
GetFileSize
"Want to Save the Changes?..." <- MS Office Word
Y
closes
 
S

src

Check that - the second save dialog box is the MS Word dialog. Does
anyone know why the regular save dialog is triggering even after my
custom one has saved the file?

Should be:
When I make changes then quit the file, I get the message boxes
indicating the sequence:
DocumentBeforeClose
"Want Me to Save Changes?..." <- my custom dialog
Y
Saving and Quitting
GetFileSize
"Want to Save the Changes?..." <- MS Office Word
Y
closes
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top