I have a pandas dataframe df
. I then save it as an excel file Report.xlsm
and then I add VBA code to filter this data.
The code runs well. However, when I execute the VBA code, Excel always shows the message box to ask to save the Report.xlsm. Does anyone know how to get rid of this message and automatically save it, rahter than having to click save?
If I do not click save, the excel file will be broken and the code will be crash too.
Microsoft Excel, ask to save file notification
Python Code:
desktop = os.path.normpath(os.path.expanduser("~/Desktop"))
# Generate excel file
writer = pd.ExcelWriter(desktop + '\Report.xlsx', engine = 'xlsxwriter')
df.to_excel(writer, index = False, sheet_name = 'Report')
workbook = writer.book
workbook.filename = desktop + '\Report.xlsm'
# Add VBA code to new excel report - Report.xlmx from vbaProject.bin
workbook.add_vba_project('vbaProject.bin')
writer.save()
# RUN MACRO
exel = win32com.client.Dispatch('Excel.Application')
exel.Workbooks.Open(Filename = desktop + '\Report.xlsm' , ReadOnly=1)
xl.Application.Run("Macro1")
xl.Application.Quit()
del xl