[英]How to set COM object to call Python script using Excel VBA?
我在装有 Office 365 Pro Plus 的 Azure VM 上运行 Windows 10。
我正在尝试设置一个 COM 对象来调用 Python 脚本http://exceldevelopmentplatform.blogspot.com/2018/01/calling-python-class-using-vba-with-com.html
我得到了 clsid
pip install pywin32 -> [python shell] import pythoncom -> print(pythoncom.CreateGuid())
所以我的 Python 脚本非常简单。
要测试它是否有效:
class PythonClass1(object):
_reg_clsid_= "{<Guid from script above here>}"
_reg_progid_= 'PythonLib1.PythonClass1'
_public_methods_ = ['Greeting']
def Greeting(self):
print("this work?")
return "Hello World"
if __name__=='__main__':
print("Registering COM server...")
import win32com.server.register
win32com.server.register.UseCommandLine(PythonClass1)
我试图从 Excel 中的 VBA 调用它:
Sub TestingButton_Click()
Dim objPython As Object
Set objPython = CreateObject("PythonLib1.PythonClass1")
Debug.Print objPython.Greeting()
End Sub
我的光标上有一个加载微调器大约五秒钟,然后 Excel 崩溃并重新启动,左侧有一个版本恢复栏,就像 Excel 正常崩溃时一样。
我在 VBA 和 Python 脚本中都尝试了 debug.print-ing。 win32traceutil.py
中没有任何win32traceutil.py
,没有错误。
导入模块pythoncom后,在类中添加_reg_clsctx_ = pythoncom.CLSCTX_LOCAL_SERVER
pythoncom.CLSCTX_LOCAL_SERVER。 那为我修好了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.