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!
> After much head scratching, I was able to find a solution at:
>
> http://groups.google.com/group/microsoft.public.word.vba.general/browse_thread/t
hread/be63415845e4d7e4/b3a57e54331920f1?lnk=gst&q=intercept&rnum=77#b3a57e543319
20f1
src - 20 Dec 2006 06:06 GMT
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
> OK, so now I'm getting the strange behaviour that I'm being asked if I
> want to save twice.
[quoted text clipped - 105 lines]
> >
> > http://groups.google.com/group/microsoft.public.word.vba.general/browse_thread/t
hread/be63415845e4d7e4/b3a57e54331920f1?lnk=gst&q=intercept&rnum=77#b3a57e543319
20f1
src - 20 Dec 2006 06:07 GMT
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