Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / December 2006

Tip: Looking for answers? Try searching our database.

How to intercept Close Event from Global Addin?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
src - 20 Dec 2006 04:08 GMT
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
-----------------
src - 20 Dec 2006 04:42 GMT
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


> I've got a routine to check the file size of documents as they are
> closed and suggest compressing pictures.
[quoted text clipped - 77 lines]
> End Sub
> -----------------
src - 20 Dec 2006 05:51 GMT
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
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.