Search code examples
pythonexcelmacroswin32com

How to stop pop when calling macro from python


I have macro code in an excel file and I call this macro using a python code but everytime I call this code it gives me popup(below) which I want avoid as the code is executed from remote machine and the excel file popup is not accessible to the user.

Code:

import win32com.client
def insert_column():
        fName= 'F:\\Reports\\Logs\\PERF_RESULTS.xlsm'
        xlApp = win32com.client.Dispatch("Excel.Application")
        fTest = xlApp.Workbooks.Open(fName,ReadOnly=1)
        macName = fTest.Name + '!' +'Insert_Column'
        xlApp.Application.Run(macName)
        xlApp.Application.Save()
        xlApp.Application.Quit()
        xlApp = None

Popup image


Solution

  • The Excel Application object has a DisplayAlerts property that can be set equal to False. I'm not sure of the exact syntax that you would use in calling it from Python but something like

    xlApp.DisplayAlerts = False
    

    prior to the line

    fTest = xlApp.Workbooks.Open(fName,ReadOnly=1)
    

    should suppress that popup.