[英]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.