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()
2
  • You should probably update your VBA Macro with code which silences those dialogs: stackoverflow.com/questions/25088225/… Commented Jun 21, 2018 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. Commented Jun 21, 2018 at 14:48

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.