Re: How to correctly handle events using win32com ?
Mark Hammond <[email protected]> Fri, 29 Nov 2024 14:10:42 -0500
| Newsgroups | gmane.comp.python.windows |
|---|---|
| Message-ID | <[email protected]> |
This is a multi-part message in MIME format.
--===============6999756160601277415==
Content-Type: multipart/alternative;
boundary="------------vEVcgVoidZVOHy0aQ0rMF7Ap"
Content-Language: en-US
This is a multi-part message in MIME format.
--------------vEVcgVoidZVOHy0aQ0rMF7Ap
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
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
> [email protected]
> https://mail.python.org/mailman/listinfo/python-win32
--------------vEVcgVoidZVOHy0aQ0rMF7Ap
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: 8bit
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<p><tt>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.</tt></p>
<p><tt><br>
</tt></p>
<div class="moz-cite-prefix">On 2024-11-29 12:17 p.m., Gauthier
ABRIAL wrote:<br>
</div>
<blockquote type="cite"
cite="mid:SJ2PR16MB5986E919FA3D3E0BC4F6BB19E22A2@SJ2PR16MB5986.namprd16.prod.outlook.com">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style type="text/css" style="display:none;">P {margin-top:0;margin-bottom:0;}</style>
<div class="elementToProof"
style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Hello,</div>
<div class="elementToProof"
style="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 win32com. If I take the example of a very simple Excel
automation, I tested two things.</div>
<ul
data-editing-info="{"applyListStyleFromLevel":false,"unorderedStyleType":2}">
<li
style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0); list-style-type: "- ";">
<div class="elementToProof">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="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0); list-style-type: "- ";">
<div class="elementToProof" style="margin: 0px;">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.</div>
</li>
</ul>
<div class="elementToProof"
style="margin: 0px; font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Maybe I should use a totally different approach.</div>
<div class="elementToProof"
style="margin: 0px; font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Thanks a lot for your help.</div>
<div class="elementToProof"
style="margin: 0px; font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div class="elementToProof"
style="margin: 0px; font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
G.</div>
<div class="elementToProof"
style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div class="elementToProof"
style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
-------------------- Code1 Starts -----------------------</div>
<div class="elementToProof"
style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
import win32com.client as win32</div>
<div
style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
import pythoncom</div>
<div
style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div
style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
#The event handlers</div>
<div
style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
class wbEvents:</div>
<div
style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
def OnBeforeClose(self, Cancel):</div>
<div
style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
print('Closing Workbook')</div>
<div
style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
# Send WM_QUIT here ?</div>
<div
style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div
style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
xlApp = win32.Dispatch("Excel.Application") #Open Excel</div>
<div
style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
xlApp.Visible=True #Make Excel Visible</div>
<div
style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
xlwb = xlApp.Workbooks.Add() #Create a workbook</div>
<div
style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
ws=xlwb.Sheets("Sheet1") #Get the first worksheet</div>
<div
style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
xl_events=win32.WithEvents(xlwb,wbEvents) #Add and Event handler</div>
<div
style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
pythoncom.PumpMessages()</div>
<div class="elementToProof"
style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
-------------------- Code1 Ends -----------------------</div>
<div class="elementToProof"
style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div class="elementToProof"
style="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="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="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
import pythoncom</div>
<div
style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
import time</div>
<div
style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div
style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
#The event handlers</div>
<div
style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
class wbEvents:</div>
<div
style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
def OnBeforeClose(self, Cancel):</div>
<div
style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
print('Closing Workbook')</div>
<div
style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
global keepOpen</div>
<div
style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
keepOpen = False</div>
<div
style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div
style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
xlApp = win32.Dispatch("Excel.Application") #Open Excel</div>
<div
style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
xlApp.Visible=True #Make Excel Visible</div>
<div
style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
xlwb = xlApp.Workbooks.Add() #Create a workbook</div>
<div
style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
ws=xlwb.Sheets("Sheet1") #Get the first worksheet</div>
<div
style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
xl_events=win32.WithEvents(xlwb,wbEvents) #Add and Event handler</div>
<div
style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
# define initalizer</div>
<div
style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
keepOpen = True</div>
<div
style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
while keepOpen:</div>
<div
style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
time.sleep(0.1)</div>
<div
style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
pythoncom.PumpWaitingMessages()</div>
<div class="elementToProof"
style="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 class="elementToProof"
style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<br>
<fieldset class="moz-mime-attachment-header"></fieldset>
<pre wrap="" class="moz-quote-pre">_______________________________________________
python-win32 mailing list
<a class="moz-txt-link-abbreviated" href="mailto:[email protected]">[email protected]</a>
<a class="moz-txt-link-freetext" href="https://mail.python.org/mailman/listinfo/python-win32">https://mail.python.org/mailman/listinfo/python-win32</a>
</pre>
</blockquote>
</body>
</html>
--------------vEVcgVoidZVOHy0aQ0rMF7Ap--
--===============6999756160601277415==
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
--===============6999756160601277415==--