【问题标题】:Error when trying to run Excel-Solver from Python over com / pywin32尝试通过 com/pywin32 从 Python 运行 Excel-Solver 时出错
【发布时间】:2013-03-06 07:16:36
【问题描述】:

我正在尝试通过 python 中的 pywin32 运行 Excels Solver 加载项:

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"

【问题讨论】:

  • 我不记得求解器是否默认安装在 Excel 中。您可能需要进入 Options 并以某种方式告诉它您需要求解器。
  • 我已经激活了求解器插件,如果我手动启动它就可以正常工作
  • COM/ActiveX 安全性?

标签: python excel winapi com solver


【解决方案1】:

我知道这已经有一年了,但如果这仍然有价值,这里的问题可能是创建 Excel OLE 对象时没有自动加载 Solver。按照设计,加载项必须先手动加载,然后才能在 Excel OLE 对象中使用。

您可以通过检查 OLE 创建的实例并转到“数据”选项卡并观察 Solver 没有出现在那里来确认它没有被加载,就像从 Windows GUI 打开 Excel 时一样。

我在尝试解决 Delphi 代码中的类似问题时发现了这一点,如下所述:

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

我希望 Python 语法会类似。

【讨论】:

    猜你喜欢
    • 2011-10-07
    • 2020-06-09
    • 1970-01-01
    • 1970-01-01
    • 2020-01-31
    • 2020-06-14
    • 1970-01-01
    • 1970-01-01
    • 2012-06-21
    相关资源
    最近更新 更多