我正在尝试使用Python来运行Excel宏并关闭Excel。我的代码如下:
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
如果我将 xl.run("File1.xlsm!WorkingFull") 去掉,我的脚本可以正常打开和关闭。 当我运行这个命令时,我会收到以下错误:
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)
我已经启用了宏并且知道它在工作簿中,问题出在哪里?
xl.run("WorkingFull")
? - SeanCxl.run(wb.WorkingFull)
,看看是否有效。 - KFleschner