简体   繁体   English

从 VBA 运行 Python 脚本没有任何反应,但是当我从 python.exe 运行它时,它起作用了

[英]Running Python script from VBA nothing happens, but when I run it from python.exe, it works

I'm running Python scrpit from VBA. The result should be a filled cell in Exel file.我从 VBA 运行 Python 脚本。结果应该是 Exel 文件中的一个填充单元格。 I tried using this VBA command:我尝试使用这个 VBA 命令:

RetVal = Shell("""C:\Python27\python.exe""" & " " & """M:\Analysis\PythonScripts\MySript.py""", vbHide)

But nothing happens as the result.但结果什么也没有发生。 However, if I run this scipt from python.exe directly, it works fine.但是,如果我直接从 python.exe 运行这个 scipt,它工作正常。 The cell in Excel file is filled with a value. Excel 文件中的单元格填充了一个值。 Which brings me to conclusion that something is wrong with VBA code.这让我得出结论,VBA 代码有问题。

Update更新

Fragment of Python Script: Python 脚本片段:

ExcelApp = win32com.client.GetActiveObject("Excel.Application")
wb = ExcelApp.Workbooks('File1.xlsm')
sheet = wb.Worksheets(u'Result')
cell = sheet.Range('B1')
cell.Value = result

I think that if you're running VBA from File1.xlsm this causes conflict between VBA and Python..like file sharing violation/permission denied because now VBA AND Python are trying to control the file at the same time.我认为,如果您从 File1.xlsm 运行 VBA,这会导致 VBA 和 Python 之间发生冲突。例如文件共享违规/权限被拒绝,因为现在 VBA 和 Python 正试图同时控制该文件。 Try running the VBA from another file that is NOT the target file and let's see how that goes.尝试从另一个不是目标文件的文件运行 VBA,让我们看看结果如何。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 python.exe 在我运行 python **.py 时崩溃,来自 windows ZDFFF0A7FA1A55C8C1A4966C19FDA445 - python.exe was crashed when I run python **.py from windows cmd 当 python.exe 设置为“以管理员身份运行”时,在 Windows 中运行 python 脚本会给出“无法使用...创建进程” - Running a python script in Windows gives "Unable to Create Process Using ..." when python.exe set to 'run as administrator' 获取从特定 python.exe 路径运行的 vscode 调试 - get vscode debug running from a specific python.exe path VS代码:python.exe仅在从Anaconda Navigator打开时才在终端中工作 - VS Code: python.exe works in terminal only when opened from Anaconda Navigator 从Windows任务计划程序运行时如何使Python.exe保持打开状态 - How to make Python.exe stay open when running from Windows Task Scheduler 运行Fabric时如何停止关闭python.exe窗口 - How to stop python.exe window from closing when running Fabric 尝试从 C# 启动 python.exe 时出错 - Error when trying to start python.exe from C# 文件从 python.exe 运行 - File runs from python.exe 在python.exe中运行时,Python代码不同 - Python code different when run in python.exe 使用py2 exe将脚本转换为.exe,但是当我运行.exe时,什么也没发生? - converted script to .exe with py2 exe but when I run .exe nothing happens?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM