感謝這個優秀的資源,它對我有很大的幫助,但是我使用excel VBA從遠程服務器下載excel文件時遇到問題。我懷疑這是明顯的,我的代碼缺乏。從
我遇到的問題是,無論原始文件的大小或內容如何,所有下載的文件總是15KB大小,並且所有文件都具有相同的內容,這些內容似乎是從主機網站簡單複製的文本,而不是比我想下載的文件要多。我曾嘗試使用包括CSV在內的各種文件擴展名,但結果相同。
當我打開下載的文件excel表示文件格式和擴展名不匹配,然後說,由於加載過程中的問題,該文件是「缺少文件C:\ remote \ css \ logon.css」和「缺少文件C:\ remote \ javascript \ ramjsfx.menu.css」,這對我來說是希臘文。
Sub DownloadFilefromWeb()
Const E_OUTOFMEMORY As Long = &H8007000E
Const E_DOWNLOAD_FAILURE As Long = &H800C0002
Dim InitialName As String
Dim Msg As String
Dim RegExp As Object
Dim RetVal As Long
Dim SaveName As String
Dim SavePath As String
Dim URL As String
URL = InputBox("Enter the download URL below.", "Download from Internet")
If URL = "" Then Exit Sub
Set RegExp = CreateObject("VBScript.RegExp")
RegExp.IgnoreCase = True
RegExp.Pattern = "^(.*\/)(.+)$"
InitialName = RegExp.Replace(URL, "$2")
Set RegExp = Nothing
If InitialName = "" Or InitialName = URL Then
MsgBox ("Error - Missing File Name")
Exit Sub
End If
SavePath = Application.GetSaveAsFilename(InitialName)
If SavePath = "" Then Exit Sub
'SavePath = "C:\Users\Rob's Laptop\Documents\Test\Test3.xls"
'URL = "https://remote.picosting.co.uk/Remote/fs/files.aspx?path=%5c%5cPISBS2011%5cfiles%5cRob% 20Shaw%27s%20test%20folder%5cTest1"
RetVal = URLDownloadToFile(0&, URL, SavePath, 0&, 0&)
Select Case RetVal
Case 0
Msg = "Download Successful"
Case E_OUTOFMEMORY
Msg = "Error - Out of Mmemory"
Case E_DOWNLOAD_FAILURE
Msg = "Error - Bad URL or Connection Interrupted"
Case Else
Msg = "Unknown Error - " & RetVal
End Select
MsgBox Msg
End Sub
親切的問候
羅布
我用這個VBScript代碼雖然不是VBA。 http://vbscriptautomation.net/73/download-files-using-vbscript/ – glh 2013-04-30 09:13:53