感谢这个优秀的资源,它对我有很大的帮助,但是我在使用 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
亲切的问候
抢