Python Forum

Full Version: Run macros of excel sheet from python scripts
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,
I am getting the following error while running the macro using python scripts

Error:
Traceback (most recent call last): File "C:\Users\username\AppData\Local\Programs\Python\Python37\macro_call.py", line 25, in <module> excel.Run('workbook1.xlsm!Grey Scaling.OpenWorkbook()') # This runs the macro that is on Sheet1 File "C:\Users\sri\AppData\Local\Temp\gen_py\3.7\00020813-0000-0000-C000-000000000046x0x1x7\_Application.py", line 376, in Run , Arg26, Arg27, Arg28, Arg29, Arg30 File "C:\Users\sri\AppData\Local\Programs\Python\Python37\lib\site-packages\win32com\client\__init__.py", line 467, in _ApplyTypes_ self._oleobj_.InvokeTypes(dispid, 0, wFlags, retType, argTypes, *args), pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, 'Microsoft Excel', "Cannot run the macro 'workbook1.xlsm!Grey Scaling.OpenWorkbook()'. The macro may not be available in this workbook or all macros may be disabled.", 'xlmain11.chm', 0, -2146827284), None)
Following is the code used:
import win32com.client as win32
excel = win32.Dispatch("Excel.Application") # create an instance of Excel
book = excel.Workbooks.Open(Filename=r'C:\Users\workbook1.xlsm')
excel.Run('workbook1.xlsm!Grey Scaling.OpenWorkbook') # This runs the macro that is on Sheet1 whose name is Grey Scalling
book.Save()
book.Close()
excel.Quit()
Thanks in advance for your help
try
import win32com.client as win32
excel = win32.Dispatch("Excel.Application") # create an instance of Excel
book = excel.Workbooks.Open(Filename=r'C:\Users\workbook1.xlsm')
excel.Application.Run('workbook1.xlsm!Grey Scaling.OpenWorkbook') # This runs the macro that is on Sheet1 whose name is Grey Scalling
book.Save()
book.Close()
excel.Application.Quit()
Thanks for the reply.
I tried above code still getting the same error.
Error:
Traceback (most recent call last): File "C:\Users\username\AppData\Local\Programs\Python\Python37\macro_call.py", line 25, in <module> excel.Application.Run('workbook1.xlsm!Grey Scaling.OpenWorkbook') # This runs the macro that is on Sheet1 File "C:\Users\sri\AppData\Local\Temp\gen_py\3.7\00020813-0000-0000-C000-000000000046x0x1x7\_Application.py", line 376, in Run , Arg26, Arg27, Arg28, Arg29, Arg30 File "C:\Users\sri\AppData\Local\Programs\Python\Python37\lib\site-packages\win32com\client\__init__.py", line 467, in _ApplyTypes_ self._oleobj_.InvokeTypes(dispid, 0, wFlags, retType, argTypes, *args), pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, 'Microsoft Excel', "Cannot run the macro 'workbook1.xlsm!Grey Scaling.OpenWorkbook'. The macro may not be available in this workbook or all macros may be disabled.", 'xlmain11.chm', 0, -2146827284), None)
what is the macro security setting?