[python-win32] launching Word with COM..how to tell when it's closed?

David Primmer dave@primco.org
Wed, 10 Apr 2002 08:26:55 -0700


I'm still working on this problem but I think using the
DocumentBeforeClose event in Word to send a message to users would be
sufficient.

However, there are a few problems with how I set this up from python.
First, I'm editing html pages so I don't have a template that I can
store macros / modules in. how do I copy a macro to the Word Doc at
runtime and then call it from python? I've read that you can do
Modules.Add() or something but I've got nothing else to go on. It says I
need to do a Class Module for the event handler code.

I have followed the Word instructions for implementing
DocumentBeforeClose and I have it working.

I'm thinking that since I have a word application object already in
python I don't have to do all the Class Module stuff that I did within
Word but this is a guess. However, it does say that I need to " declare
an object of type Application with events" Is this any different, and if
so, is there a special way to declare this kind of app object? 

Once I have that, I'm guessing that I can do the
Register_Event_Handler() from within python. Or maybe I can do it all
from within python????


Here's the DocumentBeforeClose function as it would appear in Word:

Private Sub appWord_DocumentBeforeClose _
        (ByVal Doc As Document, _
        Cancel As Boolean)

    Dim intResponse As Integer

    intResponse = MsgBox("Do you really " _
        & "want to close the document?", _
        vbYesNo)

    If intResponse = vbNo Then Cancel = True
End Sub

>From Word VBA help:

Before you can write procedures for the events of the Application
object, you must create a new class module and declare an object of type
Application with events. For example, assume that a new class module is
created and called EventClassModule. The new class module contains the
following code.

Public WithEvents App As Word.Application


>From word VBA help:

Before the procedure will run, you must connect the declared object in
the class module (App in this example) with the Application object. You
can do this with the following code from any module.

Dim X As New EventClassModule
Sub Register_Event_Handler()
    Set X.App = Word.Application
End Sub

Run the Register_Event_Handler procedure. After the procedure is run,
the App object in the class module points to the Microsoft Word
Application object, and the event procedures in the class module will
run when the events occur.


> David Primmer wrote:
> 
> >I have a wxWindows application that is starting a Word session from a
> >button click with self.w =
win32com.client.Dispatch("Word.Application")
> >and I'd like to have Word be "modal" so that the user cannot do
anything
> >in my app while Word is new Object                     . Is there any
> >event that Word would trigger that I could capture to know when the
user
> >has saved and closed Word?
> >
> >I can test for the presence of the Word object within my program but
I'm
> >guessing that would require some kind of polling mechanism (I'm not
sure
> >how to do that either).
> >
> >Would I have to make my app a COM server and then send a message from
> >Word when it closes? Don't really want to mess with something like
that.
> >
> >Any ideas are appreciated. Thanks.
> >
> >davep
> >
> >
> >
> >_______________________________________________
> >Python-win32 mailing list
> >Python-win32@python.org
> >http://mail.python.org/mailman/listinfo/python-win32
> >
> 
> 
> --
> Jens B. Jorgensen
> jens.jorgensen@tallan.com