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?.

4
  • 1
    error message looks similar to stackoverflow.com/questions/36064723/… Commented Jul 13, 2021 at 19:30
  • I'd try to comment out the last two lines in the function. Probably the file/book is closed before the macros is start. Just a guess. Commented Jul 13, 2021 at 19:36
  • 1
    xlApp_mrapp.Run("'" + wb.Name + "'!Módulo1.guardar_archivo") You already have the Application referenced by xlApp_mrapp so no need to add it. Commented Jul 13, 2021 at 20:32
  • Hi Tim Willilams. Thanks for your anwwer, You are Right, I did what you say and it works perfect. Thanks you so much. Commented Jul 13, 2021 at 21:45

2 Answers 2

1

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")
Sign up to request clarification or add additional context in comments.

Comments

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()

2 Comments

great to hear you solved the error! If this answers your original question, please feel free to accept and close your post per stackoverflow.com/help/self-answer
Your change is what I recommended in my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.