我想通過VBA中的ImageMagick將從互聯網[1]下載的圖像轉換爲JPG。到目前爲止,我已經嘗試了兩種都失敗的方法。使用ImageMagick從VBA轉換圖像
首先,我嘗試使用ImageMagickObject 1.0類型庫:
Private Sub CommandButtonOkay_Click()
Dim sURL As String, sNetFile As String, sLocalFile As String, _
cmd As String, RetVal As Integer, img As Object
Set img = New ImageMagickObject.MagickImage
sURL = UserForm1.TextBoxImgURL
sLocalFile = "C:\temp\" & UserForm1.TextBoxName
DownloadFile sURL, sLocalFile ' Function to download image from a URL and save it to a local directory
RetVal = img.Convert(sLocalFile, sLocalFile & ".jpg") '<-- This line produces the error
UserForm1.Hide
End Sub
這最終讓我有以下錯誤:
源文件(「C:\ TEMP \圖像「)存在,但是要創建的文件(」C \ temp \ image.jpg「)沒有。這與發佈的here問題非常相似,但我至今尚未找到解決方案。
其次,我試過只調用ImageMagick的使用Shell
命令:
Private Sub CommandButtonOkay_Click()
Dim sURL As String, sNetFile As String, sLocalFile As String, _
cmd As String, RetVal As Integer
sURL = UserForm1.TextBoxImgURL
sLocalFile = "C:\temp\" & UserForm1.TextBoxName
DownloadFile sURL, sLocalFile ' Function to download image from a URL and save it to a local directory
RetVal = Shell("convert.exe """ & sLocalFile & """ """ & sLocalFile & ".jpg""")
UserForm1.Hide
End Sub
當我運行此,圖像被下載就好了,但圖像不轉換,不會引發錯誤。此外,當我執行命令Shell
命令在單獨的命令窗口中執行時,轉換完全如我所料。
因此,這個問題似乎是爲什麼ImageMagick命令在它自己的命令提示符下運行時運行的很好,但是在VBA內運行時根本不工作?我不知道這是否是有用的信息,但我以編程方式從互聯網上下載圖像,所以我無法知道我得到什麼格式;我不知道這是什麼格式。不過,我用來測試這個圖片的圖片是PNG。
您是否嘗試** convert.exe **的完整路徑? – PatricK
是的。我做了,並沒有改變結果。 – tlewis3348
'Shell(「cmd.exe/c」「」「」「」&sLocalFile&「」「」「」&sLocalFile&「.jpg」「」)'? –
PatricK