[python-win32] How to correctly handle events using win32com ?

dornech dornech at gmx.de
Fri Nov 29 14:48:35 EST 2024


Hi there,

exactly, Mark is right on this.
You can use very COM object method as you use out of VBA for example.

I am working on a wrapper for a more "pythonized" access to the EXCEL
API, you may have alokk on it as soon as it is published.

BG
Chris



Am 29.11.2024 um 20:10 schrieb Mark Hammond:
>
> It's been a while since I've done any of this, but I think there's an
> "xlApp.Quit()" method you can use? If so, then you might be able to
> break out of the look when "PumpWaitingMessages()" returns 1 - that
> will mean the message queue has received a WM_QUIT.
>
>
> On 2024-11-29 12:17 p.m., Gauthier ABRIAL wrote:
>> Hello,
>> I'm looking for some advice on how to correctly handle events using
>> win32com. If I take the example of a very simple Excel automation, I
>> tested two things.
>>
>>  *
>>     Code1 below use pythoncom.PumpMessages() but I don't know how to
>>     stop it when the Excel is closing. I guess I should send a
>>     WM_QUIT message but I don't know how.
>>  *
>>     Code2 below use a while loop on pythoncom.PumpWaitingMessages()
>>     but once I stop the loop the COM server freeze as it is waiting
>>     for the messages it sends to be processed before closing. I guess
>>     I should pump all the remaining messages but I don't know how.
>>
>> Maybe I should use a totally different approach.
>> Thanks a lot for your help.
>>
>> G.
>>
>> -------------------- Code1 Starts -----------------------
>> import win32com.client as win32
>> import pythoncom
>>
>> #The event handlers
>> class wbEvents:
>>     def OnBeforeClose(self, Cancel):
>>         print('Closing Workbook')
>>         # Send WM_QUIT here ?
>>
>> xlApp = win32.Dispatch("Excel.Application") #Open Excel
>> xlApp.Visible=True #Make Excel Visible
>> xlwb = xlApp.Workbooks.Add() #Create a workbook
>> ws=xlwb.Sheets("Sheet1") #Get the first worksheet
>> xl_events=win32.WithEvents(xlwb,wbEvents) #Add and Event handler
>> pythoncom.PumpMessages()
>> -------------------- Code1 Ends -----------------------
>>
>> -------------------- Code2 Starts -----------------------
>> import win32com.client as win32
>> import pythoncom
>> import time
>>
>> #The event handlers
>> class wbEvents:
>>     def OnBeforeClose(self, Cancel):
>>         print('Closing Workbook')
>>         global keepOpen
>>         keepOpen = False
>>
>> xlApp = win32.Dispatch("Excel.Application") #Open Excel
>> xlApp.Visible=True #Make Excel Visible
>> xlwb = xlApp.Workbooks.Add() #Create a workbook
>> ws=xlwb.Sheets("Sheet1") #Get the first worksheet
>> xl_events=win32.WithEvents(xlwb,wbEvents) #Add and Event handler
>> # define initalizer
>> keepOpen = True
>> while keepOpen:
>>     time.sleep(0.1)
>>     pythoncom.PumpWaitingMessages()
>> -------------------- Code2 Ends -----------------------
>>
>>
>> _______________________________________________
>> python-win32 mailing list
>> python-win32 at python.org
>> https://mail.python.org/mailman/listinfo/python-win32
>
> _______________________________________________
> python-win32 mailing list
> python-win32 at python.org
> https://mail.python.org/mailman/listinfo/python-win32
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.python.org/pipermail/python-win32/attachments/20241129/7937b33c/attachment.html>


More information about the python-win32 mailing list