我試圖在VBA Excel中使用Shell腳本在命令提示符上執行命令。當我在命令提示符下執行此命令時,它通過打印一個數字作爲輸出。但是當我在腳本中調用相同的命令時,它不會在消息框中顯示此數字。調用命令提示符並通過VBA中的Shell Exec執行命令
Sub Button6_Click()
Dim oShell As Object
Dim objExecObject As Object
Set oShell = CreateObject("WScript.Shell")
Set objExecObject = oShell.Exec("cmd /c convert test2.jpg -fill black +opaque white -fill white -opaque white -print %[fx:w*h*mean]")
MsgBox (objExecObject.StdOut.ReadAll)
End Sub
我認爲問題與Exec函數中的參數有多個單詞。所以我試圖通過串聯一個字符串如下所示。但是這也行不通。
Dim command As String
command = "convert test2.jpg -fill black +opaque white -fill white -opaque white -print %[fx:w*h*mean] null:"
Set objExecObject = oShell.Exec("cmd /c " & command)
似乎沒有運行時錯誤,並且消息框總是返回空白而不打印數字。 FOR CLARITY,convert命令是ImageMagick提供的一個函數。
請告訴我如何在命令提示符下運行convert命令,並在消息框中輸出命令提示符。
非常感謝。
這工作。 Set objExecObject = oShell.Exec(「cmd/c convert test2.jpg -fill black + opaque white -fill white -opaque white -print%[fx:w * h * mean] null:」) null的命令是問題。還要確保文件的路徑是絕對的,在我的情況下不是問題。 – user2048724 2014-09-29 15:04:55