home *** CD-ROM | disk | FTP | other *** search
/ PC World 2002 May / PCWorld_2002-05_cd.bin / Software / TemaCD / activepython / ActivePython-2.1.1.msi / Python21_win32com_test_testMSOfficeEvents.py < prev    next >
Encoding:
Python Source  |  2001-07-26  |  3.3 KB  |  99 lines

  1. # OfficeEvents - test/demonstrate events with Word and Excel.
  2. from win32com.client import DispatchWithEvents, Dispatch
  3. import msvcrt, pythoncom
  4. import time, sys
  5. import types
  6.  
  7. import threading
  8. stopEvent = threading.Event()
  9.  
  10. def TestExcel():
  11.     class ExcelEvents:
  12.         def OnNewWorkbook(self, wb):
  13.             if type(wb) != types.InstanceType:
  14.                 raise RuntimeError, "The transformer doesnt appear to have translated this for us!"
  15.             self.seen_events["OnNewWorkbook"] = None
  16.         def OnWindowActivate(self, wb, wn):
  17.             if type(wb) != types.InstanceType or type(wn) != types.InstanceType:
  18.                 raise RuntimeError, "The transformer doesnt appear to have translated this for us!"
  19.             self.seen_events["OnWindowActivate"] = None
  20.         def OnWindowDeactivate(self, wb, wn):
  21.             self.seen_events["OnWindowDeactivate"] = None
  22.         def OnSheetDeactivate(self, sh):
  23.             self.seen_events["OnSheetDeactivate"] = None
  24.         def OnSheetBeforeDoubleClick(self, Sh, Target, Cancel):
  25.             if Target.Column % 2 == 0:
  26.                 print "You can double-click there..."
  27.             else:
  28.                 print "You can not double-click there..."
  29.             # This function is a void, so the result ends up in
  30.             # the only ByRef - Cancel.
  31.                 return 1
  32.  
  33.     e = DispatchWithEvents("Excel.Application", ExcelEvents)
  34.     e.seen_events = {}
  35.     e.Visible=1
  36.     e.Workbooks.Add()
  37.     print "Double-click in a few of the Excel cells..."
  38.     print "Press any key when finished with Excel, or wait 10 seconds..."
  39.     if not _WaitForFinish(e, 10):
  40.         e.Quit()
  41.     if not _CheckSeenEvents(e, ["OnNewWorkbook", "OnWindowActivate"]):
  42.         sys.exit(1)
  43.  
  44. def TestWord():
  45.     class WordEvents:
  46.         def OnDocumentChange(self):
  47.             self.seen_events["OnDocumentChange"] = None
  48.         def OnWindowActivate(self, doc, wn):
  49.             self.seen_events["OnWindowActivate"] = None
  50.         def OnQuit(self):
  51.             self.seen_events["OnQuit"] = None
  52.             stopEvent.set()
  53.  
  54.     w = DispatchWithEvents("Word.Application", WordEvents)
  55.     w.seen_events = {}
  56.     w.Visible = 1
  57.     w.Documents.Add()
  58.     print "Press any key when finished with Word, or wait 10 seconds..."
  59.     if not _WaitForFinish(w, 10):
  60.         w.Quit()
  61.     if not _CheckSeenEvents(w, ["OnDocumentChange", "OnWindowActivate"]):
  62.         sys.exit(1)
  63.  
  64. def _WaitForFinish(ob, timeout):
  65.     end = time.time() + timeout
  66.     while 1:
  67.         if msvcrt.kbhit():
  68.             msvcrt.getch()
  69.             break
  70.         pythoncom.PumpWaitingMessages()
  71.         stopEvent.wait(.2)
  72.         if stopEvent.isSet():
  73.             break
  74.         if not ob.Visible:
  75.             # Gone invisible - we need to pretend we timed
  76.             # out, so the app is quit.
  77.             return 0
  78.         if time.time() > end:
  79.             return 0
  80.     return 1
  81.  
  82. def _CheckSeenEvents(o, events):
  83.     for e in events:
  84.         if not o.seen_events.has_key(e):
  85.             print "ERROR: Expected event did not trigger", e
  86.             return 0
  87.     return 1
  88.  
  89. def test():
  90.     import sys
  91.     if "noword" not in sys.argv[1:]:
  92.         TestWord()
  93.     if "noexcel" not in sys.argv[1:]:
  94.         TestExcel()
  95.     print "Word and Excel event tests passed."
  96.  
  97. if __name__=='__main__':
  98.     test()
  99.