def OnWindowActivate(self, Wb=defaultNamedNotOptArg, Wn=defaultNamedNotOptArg): """ OnWindowActivate is responsible for launching the right workbook handler. IMPORTANT : Workbook apps are responsible for their own termination. :param Wb: Workbook object :param Wn: Window object """ print("{} OnWindowActivate {} {}".format(self.name, Wb.Name, Wn.Caption)) wb_thread = launch_wb_app(Wb) if wb_thread is not None: self.wb_app_thread_list.append(wb_thread) win32event.SetEvent(self.event)
def run(self): print('WorkbookAppHandler {} launched !'.format(self.name)) print('WorkbookAppHandler {} enabled apps : {}' .format(self.name, ' '.join(x for x in transitionconfig.app_get_enabled_list() if x))) try: if self._waitExcelVisible is False and self.xlApp.Visible == 0: print("WorkbookAppHandler {} : Excel wasn't running... Exiting...".format(self.name)) self.xlApp.Quit() else: if self.xlApp.EnableEvents is False: print("WorkbookAppHandler {} : Enabling Events !".format(self.name)) self.xlApp.EnableEvents = True self.xlApp = DispatchWithEvents(self.xlApp, WorkbookHandlerEvents) self.xlApp.name = "WorkbookAppHandler ExcelEvent" self.xlApp.wb_app_thread_list = self.wb_app_thread_list # open workbook apps for already opened workbooks for wb in self.xlApp.Workbooks: wb_thread = launch_wb_app(wb) if wb_thread is not None: self.wb_app_thread_list.append(wb_thread) # Main loop. Will stop at excel termination. See TransitionMain.OnDisconnection while self._ask_quit is False: win32event.WaitForSingleObject(self.xlApp.event, WAIT_FOR_EVENT_MSEC) print("WorkbookAppHandler {} is terminating...".format(self.name)) # kill opened workbook apps for wb_thread in self.wb_app_thread_list: if wb_thread in threading.enumerate(): print("WorkbookAppHandler {} : Killing {}...".format(self.name, wb_thread.name)) wb_thread.quit() except KeyboardInterrupt: print("WorkbookAppHandler {} : interruption exception".format(self.name), "intercepted (termination asked) !") except pythoncom.com_error as details: print("WorkbookAppHandler {} Exception (com_error) : {}".format(self.name, details)) # except Exception as e: # print("WorkbookAppHandler {} Exception".format(self.name, e)) self.xlApp = None print("WorkbookAppHandler {} terminated...".format(self.name))