Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Run macros of excel sheet from python scripts
#1
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
Quote
#2
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()
Quote
#3
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)
Quote
#4
what is the macro security setting?
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  autostart python scripts in background (Windows10) john36 4 208 Oct-01-2019, 01:36 PM
Last Post: john36
  Copy data from different workbooks into Master sheet with Python Fatman003 0 105 Aug-27-2019, 07:36 AM
Last Post: Fatman003
  Python code to copy data from multiple workbooks into master sheet Fatman003 2 198 Aug-21-2019, 11:23 AM
Last Post: Fatman003
  iterate read data from excel sheet jp2017 1 294 Jun-19-2019, 07:45 PM
Last Post: micseydel
  Does XLRD have a method to extract embedded objects (pdf) from an excel sheet? matthewme 1 259 Jun-13-2019, 10:00 PM
Last Post: Larz60+
  win32com Trying to open a xlmb file with macros diegoctn 3 585 Jan-16-2019, 11:26 AM
Last Post: diegoctn
  Save and Close and Excel sheet with pre defined name Vinci141 1 479 Nov-28-2018, 06:14 AM
Last Post: Vinci141
  Possible to run Python scripts from oracle? dglass 8 1,351 Sep-06-2018, 05:14 PM
Last Post: dglass
  Keyboard Maestro has problem importing python scripts bobsmith76 3 710 Aug-29-2018, 08:10 PM
Last Post: bobsmith76
  Python app built by converting visual basic macros IanR 5 1,107 May-26-2018, 09:46 AM
Last Post: Larz60+

Forum Jump:


Users browsing this thread: 2 Guest(s)