从VBA运行Python脚本,检查'RetVal = Shell'命令是否有效

时间:2019-10-11 14:53:58

标签: python excel vba shell

原始问题

当我进入C:\ Users \ HP \ Documents \ Python脚本并单击EbayWebScraper.py时,脚本将运行。大约需要30秒,但输出正确。

我想从Excel / VBA中运行此脚本。我尝试使用代码“ RetVal = Shell”并以6种不同方式编写它。在6次失败的尝试之后,我将代码转换为注释。

Sub Python()

'RetVal = Shell("python.exe C:\Users\HP\Documents\Python Scripts\EbayWebScraper.py", vbNormalFocus)


'RetVal = Shell("C:\Users\HP\AppData\Local\Programs\Python\Python37-32\python.exe " & "C:\Users\HP\Documents\Python Scripts\EbayWebScraper.py")

'RetVal = Shell("C:\Users\HP\AppData\Local\Programs\Python\Python37-32\python.exe C:\Users\HP\Documents\Python Scripts\EbayWebScraper.py")

'RetVal = Shell("C:\Users\HP\AppData\Local\Programs\Python\Python37-32\python.exe C:\Users\HP\Documents\Python Scripts\EbayWebScraper.py", vbNormalFocus)


'RetVal = Shell("python.exe" & "C:\Users\HP\Documents\Python Scripts\EbayWebScraper.py", vbNormalFocus)

'RetVal = Shell("python.exe" & "C:\Users\HP\Documents\Python Scripts\EbayWebScraper.py")


End Sub

我是否需要以某种方式放慢VBA宏的速度?是VBA宏工作太快并且没有给Python脚本足够的时间吗?

我读了这个VBA Macro running too fast并尝试了

Application Wait (Now + TimeValue (0:00:10")).

不幸的是,实验似乎没有效果。

我使用Loom录制屏幕,然后回放录制内容,并更好地查看CMD中出现的任何错误消息。不幸的是,据我所知,发生的每条错误消息都已修复。

更新1

我不知道我是否真的很愚蠢。据我所知,我提供了正确的文件目录。链接“ Windows资源管理器文件目录”是我拍摄的屏幕截图。

Microsoft Windows [Version 10.0.17134.165]
(c) 2018 Microsoft Corporation. All rights reserved.

C:\Users\HP>python C:\Users\HP\Documents\Python Scripts\EbayWebScraper.py
python: can't open file 'C:\Users\HP\Documents\Python': [Errno 2] No such file or directory
>

Windows Explorer File Directory

更新2

这也失败了。

Microsoft Windows [Version 10.0.17134.165]
(c) 2018 Microsoft Corporation. All rights reserved.

C:\Users\HP>Documents\Python Scripts\EbayWebScraper.py
'Documents\Python' is not recognized as an internal or external command,
operable program or batch file.

更新3

当我运行这段代码(从Wait for Shell to finish, then format cells - synchronously execute a command获得)时-

Sub Python2()

Dim wsh As Object
Set wsh = VBA.CreateObject("WScript.Shell")
Dim waitOnReturn As Boolean: waitOnReturn = True
Dim windowStyle As Integer: windowStyle = 1
Dim errorCode As Long

errorCode = wsh.Run("C:\Users\HP\Documents\Python Scripts\EbayWebScraper.py", windowStyle, waitOnReturn)

If errorCode = 0 Then
    MsgBox "Done! No error to report."
Else
    MsgBox "Program exited with error code " & errorCode & "."
End If

End Sub

我得到了错误-

运行时错误'-2147024894(80070002)': 对象“ IWshShell3”的方法“运行”失败

更新4

我正在看Error running Shell object / commands through Excel VBA

更新5

此代码终于可以工作了(我从Update 4的链接中获得了代码)。

    Sub Python3()

Dim wsh As Object
Set wsh = VBA.CreateObject("WScript.Shell")
Dim waitOnReturn As Boolean: waitOnReturn = True
Dim windowStyle As Integer: windowStyle = 1
Dim errorCode As Long

errorCode = wsh.Run(Chr(34) & "C:\Users\HP\Documents\Python Scripts" & "\EbayWebScraper.py" & Chr(34))

If errorCode = 0 Then
    MsgBox "Done! No error to report."
Else
    MsgBox "Program exited with error code " & errorCode & "."
End If

End Sub

0 个答案:

没有答案