Re: How to correctly handle events using win32com ?

Gauthier ABRIAL <[email protected]> Fri, 29 Nov 2024 20:01:39 +0000
Newsgroups gmane.comp.python.windows
Message-ID <SJ2PR16MB59867716E7840711B723902BE22A2@SJ2PR16MB5986.namprd16.prod.outlook.com>
--===============0085037352699932681==
Content-Language: fr-CA
Content-Type: multipart/alternative;
	boundary="_000_SJ2PR16MB59867716E7840711B723902BE22A2SJ2PR16MB5986namp_"

--_000_SJ2PR16MB59867716E7840711B723902BE22A2SJ2PR16MB5986namp_
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

For some reasons the PumpWaitingMessages() is never returning 1, even if it=
 seems that Excel has been properly closed and the process ended.

My problem is that once I have called "WithEvents", if I stop calling PumpW=
aitingMessages() Excel is freezing because it's waiting for the events to b=
e processed.
Is there any way to "undo" what "WithEvents" is doing so that the server (E=
xcel) is not waiting for the message to be pumped (and do not freeze if I s=
top calling PumpWaitingMessages()) ?

Thanks a lot for your help,
G.

________________________________
De : Mark Hammond <[email protected]>
Envoy=E9 : 29 novembre 2024 14:10
=C0 : Gauthier ABRIAL <[email protected]>; [email protected] <pyth=
[email protected]>
Objet : Re: [python-win32] How to correctly handle events using win32com ?


It's been a while since I've done any of this, but I think there's an "xlAp=
p.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 messa=
ge 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 win32co=
m. If I take the example of a very simple Excel automation, I tested two th=
ings.

  *
Code1 below use pythoncom.PumpMessages() but I don't know how to stop it wh=
en 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 se=
nds 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 =3D win32.Dispatch("Excel.Application") #Open Excel
xlApp.Visible=3DTrue #Make Excel Visible
xlwb =3D xlApp.Workbooks.Add() #Create a workbook
ws=3Dxlwb.Sheets("Sheet1") #Get the first worksheet
xl_events=3Dwin32.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 =3D False

xlApp =3D win32.Dispatch("Excel.Application") #Open Excel
xlApp.Visible=3DTrue #Make Excel Visible
xlwb =3D xlApp.Workbooks.Add() #Create a workbook
ws=3Dxlwb.Sheets("Sheet1") #Get the first worksheet
xl_events=3Dwin32.WithEvents(xlwb,wbEvents) #Add and Event handler
# define initalizer
keepOpen =3D True
while keepOpen:
    time.sleep(0.1)
    pythoncom.PumpWaitingMessages()
-------------------- Code2 Ends -----------------------




_______________________________________________
python-win32 mailing list
[email protected]<mailto:[email protected]>
https://mail.python.org/mailman/listinfo/python-win32


--_000_SJ2PR16MB59867716E7840711B723902BE22A2SJ2PR16MB5986namp_
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<html>
<head>
<meta http-equiv=3D"Content-Type" content=3D"text/html; charset=3Diso-8859-=
1">
<style type=3D"text/css" style=3D"display:none;"> P {margin-top:0;margin-bo=
ttom:0;} </style>
</head>
<body dir=3D"ltr">
<div class=3D"elementToProof" style=3D"font-family: Calibri, Helvetica, san=
s-serif; font-size: 12pt; color: rgb(0, 0, 0);">
For some reasons the PumpWaitingMessages()&nbsp;is never returning 1, even =
if it seems that Excel has been properly closed and the process ended.</div=
>
<div class=3D"elementToProof" style=3D"font-family: Calibri, Helvetica, san=
s-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div class=3D"elementToProof" style=3D"font-family: Calibri, Helvetica, san=
s-serif; font-size: 12pt; color: rgb(0, 0, 0);">
My problem is that once I have called &quot;WithEvents&quot;, if I stop cal=
ling PumpWaitingMessages() Excel is freezing because it's waiting for the e=
vents to be processed.&nbsp;</div>
<div class=3D"elementToProof" style=3D"font-family: Calibri, Helvetica, san=
s-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Is there any way to &quot;undo&quot; what &quot;WithEvents&quot; is doing s=
o that the server (Excel) is not waiting for the message to be pumped (and =
do not freeze if I stop calling PumpWaitingMessages()) ?</div>
<div class=3D"elementToProof" style=3D"font-family: Calibri, Helvetica, san=
s-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div class=3D"elementToProof" style=3D"font-family: Calibri, Helvetica, san=
s-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Thanks a lot for your help,</div>
<div class=3D"elementToProof" style=3D"font-family: Calibri, Helvetica, san=
s-serif; font-size: 12pt; color: rgb(0, 0, 0);">
G.</div>
<div id=3D"appendonsend"></div>
<div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;=
 color: rgb(0, 0, 0);">
<br>
</div>
<hr style=3D"display: inline-block; width: 98%;">
<div id=3D"divRplyFwdMsg" dir=3D"ltr"><span style=3D"font-family: Calibri, =
sans-serif; font-size: 11pt; color: rgb(0, 0, 0);"><b>De :</b>&nbsp;Mark Ha=
mmond &lt;[email protected]&gt;<br>
<b>Envoy=E9 :</b>&nbsp;29 novembre 2024 14:10<br>
<b>=C0 :</b>&nbsp;Gauthier ABRIAL &lt;[email protected]&gt;; python-win3=
[email protected] &lt;[email protected]&gt;<br>
<b>Objet :</b>&nbsp;Re: [python-win32] How to correctly handle events using=
 win32com ?</span>
<div>&nbsp;</div>
</div>
<p class=3D"elementToProof" style=3D"margin-top: 0px; margin-bottom: 0px;">=
<tt></p>
<p class=3D"elementToProof" style=3D"margin-top: 0px; margin-bottom: 0px;">=
It's been a while since I've done any of this, but I think there's an &quot=
;xlApp.Quit()&quot; method you can use? If so, then you might be able to br=
eak out of the look when &quot;PumpWaitingMessages()&quot;
 returns 1 - that will mean the message queue has received a WM_QUIT.</p>
</tt>
<p></p>
<p style=3D"margin-top: 0px; margin-bottom: 0px;"><tt><br>
</tt></p>
<div>On 2024-11-29 12:17 p.m., Gauthier ABRIAL wrote:</div>
<blockquote>
<div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;=
 color: rgb(0, 0, 0);">
Hello,</div>
<div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;=
 color: rgb(0, 0, 0);">
I'm looking for some advice on how to correctly handle events using win32co=
m. If I take the example of a very simple Excel automation, I tested two th=
ings.</div>
<ul data-editing-info=3D"{&quot;applyListStyleFromLevel&quot;:false,&quot;u=
norderedStyleType&quot;:2}">
<li style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; =
color: rgb(0, 0, 0); list-style-type: &quot;- &quot;;">
<div>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.</div>
</li><li style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 1=
2pt; color: rgb(0, 0, 0); list-style-type: &quot;- &quot;;">
<div style=3D"margin: 0px;">Code2 below use a while loop on pythoncom.PumpW=
aitingMessages() but once I stop the loop the COM server freeze as it is wa=
iting for the messages it sends to be processed before closing. I guess I s=
hould pump all the remaining messages
 but I don't know how.</div>
</li></ul>
<div style=3D"margin: 0px; font-family: Calibri, Helvetica, sans-serif; fon=
t-size: 12pt; color: rgb(0, 0, 0);">
Maybe I should use a totally different approach.</div>
<div style=3D"margin: 0px; font-family: Calibri, Helvetica, sans-serif; fon=
t-size: 12pt; color: rgb(0, 0, 0);">
Thanks a lot for your help.</div>
<div style=3D"margin: 0px; font-family: Calibri, Helvetica, sans-serif; fon=
t-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div style=3D"margin: 0px; font-family: Calibri, Helvetica, sans-serif; fon=
t-size: 12pt; color: rgb(0, 0, 0);">
G.</div>
<div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;=
 color: rgb(0, 0, 0);">
<br>
</div>
<div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;=
 color: rgb(0, 0, 0);">
-------------------- Code1 Starts -----------------------</div>
<div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;=
 color: rgb(0, 0, 0);">
import win32com.client as win32</div>
<div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;=
 color: rgb(0, 0, 0);">
import pythoncom</div>
<div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;=
 color: rgb(0, 0, 0);">
<br>
</div>
<div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;=
 color: rgb(0, 0, 0);">
#The event handlers</div>
<div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;=
 color: rgb(0, 0, 0);">
class wbEvents:</div>
<div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;=
 color: rgb(0, 0, 0);">
&nbsp; &nbsp; def OnBeforeClose(self, Cancel):</div>
<div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;=
 color: rgb(0, 0, 0);">
&nbsp; &nbsp; &nbsp; &nbsp; print('Closing Workbook')</div>
<div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;=
 color: rgb(0, 0, 0);">
&nbsp; &nbsp; &nbsp; &nbsp; # Send WM_QUIT here ?</div>
<div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;=
 color: rgb(0, 0, 0);">
<br>
</div>
<div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;=
 color: rgb(0, 0, 0);">
xlApp =3D win32.Dispatch(&quot;Excel.Application&quot;) #Open Excel</div>
<div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;=
 color: rgb(0, 0, 0);">
xlApp.Visible=3DTrue #Make Excel Visible</div>
<div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;=
 color: rgb(0, 0, 0);">
xlwb =3D xlApp.Workbooks.Add() #Create a workbook</div>
<div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;=
 color: rgb(0, 0, 0);">
ws=3Dxlwb.Sheets(&quot;Sheet1&quot;) #Get the first worksheet</div>
<div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;=
 color: rgb(0, 0, 0);">
xl_events=3Dwin32.WithEvents(xlwb,wbEvents) #Add and Event handler</div>
<div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;=
 color: rgb(0, 0, 0);">
pythoncom.PumpMessages()</div>
<div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;=
 color: rgb(0, 0, 0);">
-------------------- Code1 Ends -----------------------</div>
<div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;=
 color: rgb(0, 0, 0);">
<br>
</div>
<div style=3D"text-align: left; text-indent: 0px; margin: 0px; font-family:=
 Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
-------------------- Code2 Starts -----------------------</div>
<div style=3D"text-align: left; text-indent: 0px; margin: 0px; font-family:=
 Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
import win32com.client as win32</div>
<div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;=
 color: rgb(0, 0, 0);">
import pythoncom</div>
<div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;=
 color: rgb(0, 0, 0);">
import time</div>
<div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;=
 color: rgb(0, 0, 0);">
<br>
</div>
<div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;=
 color: rgb(0, 0, 0);">
#The event handlers</div>
<div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;=
 color: rgb(0, 0, 0);">
class wbEvents:</div>
<div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;=
 color: rgb(0, 0, 0);">
&nbsp; &nbsp; def OnBeforeClose(self, Cancel):</div>
<div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;=
 color: rgb(0, 0, 0);">
&nbsp; &nbsp; &nbsp; &nbsp; print('Closing Workbook')</div>
<div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;=
 color: rgb(0, 0, 0);">
&nbsp; &nbsp; &nbsp; &nbsp; global keepOpen</div>
<div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;=
 color: rgb(0, 0, 0);">
&nbsp; &nbsp; &nbsp; &nbsp; keepOpen =3D False</div>
<div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;=
 color: rgb(0, 0, 0);">
<br>
</div>
<div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;=
 color: rgb(0, 0, 0);">
xlApp =3D win32.Dispatch(&quot;Excel.Application&quot;) #Open Excel</div>
<div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;=
 color: rgb(0, 0, 0);">
xlApp.Visible=3DTrue #Make Excel Visible</div>
<div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;=
 color: rgb(0, 0, 0);">
xlwb =3D xlApp.Workbooks.Add() #Create a workbook</div>
<div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;=
 color: rgb(0, 0, 0);">
ws=3Dxlwb.Sheets(&quot;Sheet1&quot;) #Get the first worksheet</div>
<div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;=
 color: rgb(0, 0, 0);">
xl_events=3Dwin32.WithEvents(xlwb,wbEvents) #Add and Event handler</div>
<div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;=
 color: rgb(0, 0, 0);">
# define initalizer</div>
<div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;=
 color: rgb(0, 0, 0);">
keepOpen =3D True</div>
<div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;=
 color: rgb(0, 0, 0);">
while keepOpen:</div>
<div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;=
 color: rgb(0, 0, 0);">
&nbsp; &nbsp; time.sleep(0.1)</div>
<div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;=
 color: rgb(0, 0, 0);">
&nbsp; &nbsp; pythoncom.PumpWaitingMessages()</div>
<div style=3D"text-align: left; text-indent: 0px; margin: 0px; font-family:=
 Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
-------------------- Code2 Ends -----------------------</div>
<div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;=
 color: rgb(0, 0, 0);">
<br>
</div>
<br>
<fieldset class=3D"x_moz-mime-attachment-header"></fieldset>
<pre><div>_______________________________________________=0A=
python-win32 mailing list=0A=
<a href=3D"mailto:[email protected]" id=3D"OWA495bf050-c427-831f-2854=
-26afb7ae2766" class=3D"x_moz-txt-link-abbreviated OWAAutoLink">python-win3=
[email protected]</a>=0A=
<a href=3D"https://mail.python.org/mailman/listinfo/python-win32" id=3D"OWA=
bf3231e6-d31d-3d36-23d2-391ce397a4b6" class=3D"x_moz-txt-link-freetext OWAA=
utoLink" originalsrc=3D"https://mail.python.org/mailman/listinfo/python-win=
32" data-auth=3D"Verified">https://mail.python.org/mailman/listinfo/python-=
win32</a>=0A=
</div></pre>
</blockquote>
</body>
</html>

--_000_SJ2PR16MB59867716E7840711B723902BE22A2SJ2PR16MB5986namp_--

--===============0085037352699932681==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
python-win32 mailing list
[email protected]
https://mail.python.org/mailman/listinfo/python-win32

--===============0085037352699932681==--