从网站打开下载的文件

时间:2018-03-09 16:21:49

标签: excel vba excel-vba

我能够访问该网站并单击“下载”按钮,一旦我点击它,下方的对话框就会打开,按钮打开,保存,取消。我想点击“打开”按钮,以可爱的pdf格式打印打开的文件。

如何点击“打开”按钮请帮助我。

Screen shot of dialog box

用于点击下载按钮的代码

ie2.Document.forms("ViewReferral").getElementsByClassName("notsuccess")(0).getElementsByTagName("tr").Item(1).getElementsByTagName("td").Item(3).getElementsByTagName("A").Item(0).Click

一旦我点击它,上面提到的对话框就会打开。

谢谢。

1 个答案:

答案 0 :(得分:1)

而不是试图点击浏览器上的按钮' "打开/保存/取消"您可以直接下载该文件,只要您能够通过任何抓取方法检索完整的网址(即http://......attached_doc135155.pdf)。

使用VBA

从URL下载文件
Option Explicit

Sub downloadFile(url As String, filePath As String)

    Dim WinHttpReq As Object, attempts As Integer, oStream
    attempts = 3
    On Error GoTo TryAgain
TryAgain:
    attempts = attempts - 1
    Err.Clear
    If attempts > 0 Then
        Set WinHttpReq = CreateObject("Microsoft.XMLHTTP")
        WinHttpReq.Open "GET", url, False
        WinHttpReq.send

        If WinHttpReq.Status = 200 Then
            Set oStream = CreateObject("ADODB.Stream")
            oStream.Open
            oStream.Type = 1
            oStream.Write WinHttpReq.responseBody
            oStream.SaveToFile filePath, 2 ' 1 = no overwrite, 2 = overwrite
            oStream.Close
            MsgBox "File downloaded to:" & vbLf & filePath
        End If
    Else
        MsgBox "Failed."
    End If

End Sub

Sub testDownload()
    Const testFileURL = "http://ipv4.download.thinkbroadband.com/5MB.zip"
    Const localSavePathFile = "c:\5MB_testfile.zip"
    downloadFile testFileURL, localSavePathFile
End Sub

<子>(Source