我需要通过python运行Excel宏,我总是得到以下错误 :
result = self._oleobj_.InvokeTypes(*(dispid, LCID, wFlags, retType, argTypes) + args)
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, None, None, None, 0, -2146788248), None)
在Excel中,它给出了以下错误 :
运行时错误1004: 无法运行宏"。 宏在此工作簿中可能不可用,或者所有宏都可能被禁用。
我的代码如下 :
xl=win32.Dispatch("Excel.Application")
wb=xl.Workbooks.Open(Filename="Path+MyExcelFile.xlsm", ReadOnly=1)
xl.Visible = True
time.sleep(1)
ws=wb.Worksheets("Sheet1")
ws.Cells(4,2).Value='Value1'
ws.Cells(5,2).Value='Value2'
ws.Cells(1,8).Value='Bool'
time.sleep(10)
xl.Application.Run("MyExcelFile.xlsm!macroname")
result = ws.Cells(3,10).Value
print result
xl.Application.Quit()
del xl
我通过宏安全启用了所有宏,并且没有为特定工作表定义宏。 当然,如果我在Excel中手动运行宏,宏工作正常
我试过xl.Application。运行("macroname"),然后它的工作。 我只有一个工作簿打开和一个独特的"macroname"。