0

I have a python script that uses win32com to run a VBA Macro on a Excel file. The problem is that if there is an exception in the VBA Macro, Excel will open with a dialog box asking the user to continue.

This will block the rest of the python execution until the user answer the dialogbox.

Error shown to the user

As my application will be run on a server to generate multiple Excel file, there won't be a user to close the dialog box.

How could I ignore the Excel dialog box when there is an error so my application can keep running ?

Here is my python code :

import comtypes
comtypes.CoInitialize()

import win32com.client
excel = win32com.client.DispatchEx("Excel.Application")
excel.DisplayAlerts = False
wb = excel.Workbooks.Open(Filename=excel_file)

try:
    excel.Run('ReportModule.Compute')
    wb.Save()
except Exception as e:
    logging.error('Failed to run macro for {}.'.format(excel_file))
    logging.error(e, exc_info=True)

excel.Quit()
cpaulus
  • 284
  • 1
  • 14
  • You should probably update your VBA Macro with code which silences those dialogs: https://stackoverflow.com/questions/25088225/disable-all-dialog-boxes-in-excel-while-running-vb-script – André C. Andersen Jun 21 '18 at 14:44
  • Thanks I'll look into that. But I would still prefer a more robust solution. In my case the macros will be written by different users. So if they forget/remove the safeguards, the server could still be blocked by a dialog box. – cpaulus Jun 21 '18 at 14:48

0 Answers0