0

I want to run a Macro with python. I am doing:

import win32com.client as w3c

def ejecuntar_macro():
    xlApp_mrapp = w3c.Dispatch("Excel.Application")
    pw_str = str('Plantilla123')
    mrapp = r'D:\Proyectos\Tablero estados\Tablero.xlsm'
    xlApp_mrapp.Visible = True
    xlApp_mrapp.DisplayAlerts = False 
    wb = xlApp_mrapp.Workbooks.Open(mrapp, False, False, None, pw_str)
    xlApp_mrapp.Application.Run("'" + mrapp + "'" + "!Módulo1.guardar_archivo")
    wb.Close(True)
    xlApp_mrapp.Quit()

ejecuntar_macro()

but I keep getting an error:

File ".\ejecucion.py", line 244, in ejecuntar_macro xlApp_mrapp.Application.Run("'" + mrapp + "'" + "!Módulo1.guardar_archivo") File "", line 14, in Run File "C:\Users\Ruben\AppData\Roaming\Python\Python37\site-packages\win32com\client\dynamic.py", line 314, in ApplyTypes result = self.oleobj.InvokeTypes(*(dispid, LCID, wFlags, retType, argTypes) + args) pywintypes.com_error: (-2147352567, 'Ocurrió una excepción.', (0, None, None, None, 0, -2146788248), None)

Please, Can You help me to solve it?.

braX
  • 11,506
  • 5
  • 20
  • 33

2 Answers2

0

I do not believe you need to specify the whole file path of the workbook to run the macro. You need to specify whole file path to open the workbook, obviously. But once the workbook is open then to run a macro you only need to specify the workbook name. So something like the following ...

xlApp_mrapp.Application.Run("Tablero.xlsm!Módulo1.guardar_archivo")
S Meaden
  • 8,050
  • 3
  • 34
  • 65
0

Thanks for your answers. I solved the error the follows. First, I have to say that in my macro I have a code to close the file. I removed this part.

the code is:

import win32com.client as w3c

def ejecuntar_macro():
    xlApp_mrapp = w3c.Dispatch("Excel.Application")
    pw_str = str('Plantilla123')
    mrapp = r'D:\Proyectos\Tablero estados\Tablero.xlsm'
    tablero = 'Tablero.xlsm'
    xlApp_mrapp.Visible = True
    xlApp_mrapp.DisplayAlerts = False 
    wb = xlApp_mrapp.Workbooks.Open(mrapp, False, False, None, pw_str)
    xlApp_mrapp.Application.Run("'" + tablero+ "'" + "!Módulo1.guardar_archivo")
    wb.Close(True)
    xlApp_mrapp.Quit()