3

I am trying to use python to run an excel macro and then close excel. I have the following:

 import win32com.client
 import os

 xl = win32com.client.DispatchEx("Excel.Application")
 wb = xl.workbooks.open("X:\Location\Location2\File1.xlsm")
 xl.run("File1.xlsm!WorkingFull")
 xl.Visible = True
 wb.Close(SaveChanges=1)
 xl.Quit

My script will Open and close fine if I take out the xl.run("File1.xlsm!WorkingFull") When I run this I get the following error:

Traceback (most recent call last): File "C:\Python27\File1.py", line 6, in xl.run("File1.xlsm!WorkingFull") File "", line 2, in run com_error: (-2147352567, 'Exception occurred.', (0, u'Microsoft Excel', u"Cannot run the macro 'File1.xlsm!WorkingFull'. The macro may not be available in this workbook or all macros may be disabled.", u'xlmain11.chm', 0, -2146827284), None)

I have macros enabled and I know its in the workbook, what is the problem?

3
  • 1
    have you tried reducing the call? xl.run("WorkingFull")? Commented Jul 24, 2012 at 17:47
  • Make sure your macro is not private. Also try xl.run(wb.WorkingFull) and see if that works. Commented Jul 24, 2012 at 17:48
  • worked, but I am getting the error, but it seems to run my macro. The only reason I know this is my macro saves a file off. Traceback (most recent call last): File "C:\Python27\file1.py", line 6, in <module> xl.run("WorkingFull") File "<COMObject Excel.Application>", line 2, in run com_error: (-2147352567, 'Exception occurred.', (0, None, None, None, 0, -2146788248), None) Commented Jul 24, 2012 at 17:54

1 Answer 1

5

please see below the code for running an Excel macro using python. You can find this code in this Site - Link. Use this site for other references for excel, vba and python scripts which might be helpful in the future.

from __future__ import print_function
import unittest
import os.path
import win32com.client

class ExcelMacro(unittest.TestCase):
    def test_excel_macro(self):
        try:
            xlApp = win32com.client.DispatchEx('Excel.Application')
            xlsPath = os.path.expanduser('C:\test1\test2\test3\test4\MacroFile.xlsm')
            wb = xlApp.Workbooks.Open(Filename=xlsPath)
            xlApp.Run('macroName')
            wb.Save()
            xlApp.Quit()
            print("Macro ran successfully!")
        except:
            print("Error found while running the excel macro!")
            xlApp.Quit()
if __name__ == "__main__":
    unittest.main()
Sign up to request clarification or add additional context in comments.

4 Comments

While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes.
cpBurnz - I understand your concern and I have placed the code above which will show the users how to run an excel macro utilizing python. If you could kindly vote on my answer as useful it would be greatly appreciated.
That looks much better.
I am getting "Site not found"

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.