我花了最后几个小时试图弄清楚如何使用VBA将文件保存到计算机上。我在另一个论坛上找到的代码模板似乎很有希望,除非我去桌面访问它,.csv文件看起来像页面的源代码而不是我想要的实际文件。这可能是因为当我转到URL时,它不会自动下载文件;相反,我被要求将文件保存到某个位置(因为我不知道网站上传文件的路径名)。 有没有办法改变这个代码以适应这个,或者我是否必须完全使用不同的代码?
Sub Test()
Dim FileNum As Long
Dim FileData() As Byte
Dim MyFile As String
Dim WHTTP As Object
On Error Resume Next
Set WHTTP = CreateObject("WinHTTP.WinHTTPrequest.5")
If Err.Number <> 0 Then
Set WHTTP = CreateObject("WinHTTP.WinHTTPrequest.5.1")
End If
On Error GoTo 0
MyFile = "MY_URL_HERE"
WHTTP.Open "GET", MyFile, False
WHTTP.send
FileData = WHTTP.responseBody
Set WHTTP = Nothing
If Dir("C:\Users\BLAHBLAH\Desktop", vbDirectory) = Empty Then MkDir "C:\Users\BLAHBLAH\Desktop"
FileNum = FreeFile
Open "C:\Users\BLAHBLAH\Desktop\memberdatabase.csv" For Binary Access Write As #FileNum
Put #FileNum, 1, FileData
Close #FileNum
End Sub
交叉帖子:
http://www.ozgrid.com/forum/showthread.php?t=178884
http://www.excelforum.com/excel-programming-vba-macros/925352-vba-go-to-website-and-download-file-from-save-prompt.html
答案 0 :(得分:1)
多年来我发现了save/download data
使用vba的更多方法:
URLDownloadToFile function
的{{1}}
user32 library
。为了实现此目的,您还可以将Interop.WinHttp引用添加到项目link。之后,您可以使用更简单的表示法,例如link Microsoft WinHTTP Services (Interop.WinHttp) COM library
功能。在这种情况下,我们使用COM界面打开Internet Explorer并导航到正确的站点。因此,我们必须将Save_Over_Existing_Click_Yes
(Microsoft Internet Controls
)和Interop.SHDocVw
(Microsoft HTML Object Library
)引用添加到项目中,以获得编辑器的智能感知功能。
我不喜欢这个下载方法,因为这是一个黑客的解决方法。但如果你的IE会话已经建立了认证等,这将很好地工作。由于安全问题,Internet控件的保存功能被删除。例如,请参阅:link 越新越少,你必须有正确的网址来下载你想要的东西。如果您选错了,您将下载其他内容:)
答案 1 :(得分:0)
尝试以下代码:
从here复制(未经测试)
Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Declare Function SetForegroundWindow Lib "user32" Alias "SetForegroundWindow" (ByVal hwnd As Long) As Long
Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Declare Sub Sleep Lib "kernel32" Alias "Sleep" (ByVal dwMilliseconds As Long)
Private Sub Save_Over_Existing_Click_Yes()
Dim hWnd As Long
Dim timeout As Date
Debug.Print "Save_Over_Existing_Click_Yes"
'Find the Download complete window, waiting a maximum of 30 seconds for it to appear. Timeout value is dependent on the
'size of the download, so make it longer for bigger files
timeout = Now + TimeValue("00:00:30")
Do
hWnd = FindWindow(vbNullString, "Save As")
DoEvents
Sleep 200
Loop Until hWnd Or Now > timeout
Debug.Print " Save As window "; Hex(hWnd)
If hWnd Then
'Find the child Close button
hWnd = FindWindowEx(hWnd, 0, "Button", "&Yes")
Debug.Print " Yes button "; Hex(hWnd)
End If
If hWnd Then
'Click the Close button
SetForegroundWindow (hWnd)
Sleep 600 'this sleep is required and 600 miiliseconds seems to be the minimum that works
SendMessage hWnd, BM_CLICK, 0, 0
End If
End Sub