2013-03-18 29 views
1

我試圖運行擅長求解加載通過pywin32在python搭配:錯誤當試圖在COM/pywin32運行Excel的求解器從Python的

import win32com.client 
from win32com.client import constants as c 

app = Dispatch("Excel.Application") 
app.Visible = True 
app.Workbooks.Open(r'C:\path\to\testsolver.xlsm') 
app.Run("runsolver") 

..但得到以下錯誤:

"Cannot run the macro 'runsolver'. The macro may not be available in this workbook or all macros may be disabled" 
+0

我不記得解算器是否默認安裝在Excel中。你可能需要進入選項並以某種方式告訴它你想要求解器。 – minopret 2013-03-18 01:46:57

+0

我的解算器加載項實際上已激活,並且它工作正常,如果我手動啓動它 – user990330 2013-03-18 01:59:51

+0

COM/ActiveX安全性? – 2013-03-19 02:41:00

回答

2

我意識到這是一個古老的,但萬一這仍然有價值的問題在這裏可能是解決方案不會自動加載Excel OLE對象時創建。按照設計,加載項必須手動加載,才能在Excel OLE對象中使用。

您可以通過檢查OLE創建的實例並轉到「數據」選項卡並觀察Solver沒有出現在那裏來確認它沒有加載,就像它從Windows GUI打開Excel時一樣。

我發現這一點的同時試圖這裏描述在Delphi代碼來解決類似的問題:

CreateOleObject opened workbook won't run macro containing Application.Run "Solver.xlam!..." causes error 400

我希望Python語法將是類似的。

相關問題