PyXR

c:\python24\lib\site-packages\win32 \ com \ test \ testMSOfficeEvents.py



0001 # OfficeEvents - test/demonstrate events with Word and Excel.
0002 from win32com.client import DispatchWithEvents, Dispatch
0003 import msvcrt, pythoncom
0004 import time, sys
0005 import types
0006 
0007 import threading
0008 stopEvent = threading.Event()
0009 
0010 def TestExcel():
0011     class ExcelEvents:
0012         def OnNewWorkbook(self, wb):
0013             if type(wb) != types.InstanceType:
0014                 raise RuntimeError, "The transformer doesnt appear to have translated this for us!"
0015             self.seen_events["OnNewWorkbook"] = None
0016         def OnWindowActivate(self, wb, wn):
0017             if type(wb) != types.InstanceType or type(wn) != types.InstanceType:
0018                 raise RuntimeError, "The transformer doesnt appear to have translated this for us!"
0019             self.seen_events["OnWindowActivate"] = None
0020         def OnWindowDeactivate(self, wb, wn):
0021             self.seen_events["OnWindowDeactivate"] = None
0022         def OnSheetDeactivate(self, sh):
0023             self.seen_events["OnSheetDeactivate"] = None
0024         def OnSheetBeforeDoubleClick(self, Sh, Target, Cancel):
0025             if Target.Column % 2 == 0:
0026                 print "You can double-click there..."
0027             else:
0028                 print "You can not double-click there..."
0029             # This function is a void, so the result ends up in
0030             # the only ByRef - Cancel.
0031                 return 1
0032 
0033     class WorkbookEvents:
0034         def OnActivate(self):
0035             print "workbook OnActivate"
0036         def OnBeforeRightClick(self, Target, Cancel):
0037             print "It's a Worksheet Event"
0038 
0039     e = DispatchWithEvents("Excel.Application", ExcelEvents)
0040     e.seen_events = {}
0041     e.Visible=1
0042     book = e.Workbooks.Add()
0043     book = DispatchWithEvents(book, WorkbookEvents)
0044     print "Have book", book
0045 #    sheet = e.Worksheets(1)
0046 #    sheet = DispatchWithEvents(sheet, WorksheetEvents)
0047     
0048     print "Double-click in a few of the Excel cells..."
0049     print "Press any key when finished with Excel, or wait 10 seconds..."
0050     if not _WaitForFinish(e, 10):
0051         e.Quit()
0052     if not _CheckSeenEvents(e, ["OnNewWorkbook", "OnWindowActivate"]):
0053         sys.exit(1)
0054 
0055 def TestWord():
0056     class WordEvents:
0057         def OnDocumentChange(self):
0058             self.seen_events["OnDocumentChange"] = None
0059         def OnWindowActivate(self, doc, wn):
0060             self.seen_events["OnWindowActivate"] = None
0061         def OnQuit(self):
0062             self.seen_events["OnQuit"] = None
0063             stopEvent.set()
0064 
0065     w = DispatchWithEvents("Word.Application", WordEvents)
0066     w.seen_events = {}
0067     w.Visible = 1
0068     w.Documents.Add()
0069     print "Press any key when finished with Word, or wait 10 seconds..."
0070     if not _WaitForFinish(w, 10):
0071         w.Quit()
0072     if not _CheckSeenEvents(w, ["OnDocumentChange", "OnWindowActivate"]):
0073         sys.exit(1)
0074 
0075 def _WaitForFinish(ob, timeout):
0076     end = time.time() + timeout
0077     while 1:
0078         if msvcrt.kbhit():
0079             msvcrt.getch()
0080             break
0081         pythoncom.PumpWaitingMessages()
0082         stopEvent.wait(.2)
0083         if stopEvent.isSet():
0084             stopEvent.clear()
0085             break
0086         try:
0087             if not ob.Visible:
0088                 # Gone invisible - we need to pretend we timed
0089                 # out, so the app is quit.
0090                 return 0
0091         except pythoncom.com_error:
0092             # Excel is busy (eg, editing the cell) - ignore
0093             pass
0094         if time.time() > end:
0095             return 0
0096     return 1
0097 
0098 def _CheckSeenEvents(o, events):
0099     rc = 1
0100     for e in events:
0101         if not o.seen_events.has_key(e):
0102             print "ERROR: Expected event did not trigger", e
0103             rc = 0
0104     return rc
0105 
0106 def test():
0107     import sys
0108     if "noword" not in sys.argv[1:]:
0109         TestWord()
0110     if "noexcel" not in sys.argv[1:]:
0111         TestExcel()
0112     print "Word and Excel event tests passed."
0113 
0114 if __name__=='__main__':
0115     test()
0116 

Generated by PyXR 0.9.4
SourceForge.net Logo