【问题标题】:How to click on the download botton while exploring the PDF in IE11 through excel VBA如何通过excel VBA在IE11中浏览PDF时单击下载按钮
【发布时间】:2018-09-27 10:06:24
【问题描述】:

Sub Drop_Down() Dim objIE As Object, ele As Object, opt As Object 设置 objIE = CreateObject("InternetExplorer.Application")

objIE.Visible = True
objIE.navigate "https://isgs-oas.isgs.illinois.edu/reports/rwservlet?oil_permit_activity"

Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop

Set ele = objIE.document.getElementsByTagName("INPUT")

For Each opt In ele
    If opt.getAttribute("name") = "p_YEAR" Then
        opt.Focus
        opt.Value = "2018"
        Exit For
    End If
Next opt

Set ele = objIE.document.getElementsByTagName("select")

For Each opt In ele
    If opt.getAttribute("name") = "p_MONTH" Then
        opt.Focus
        opt.Value = "January"
        Exit For
    End If
Next opt
objIE.document.forms(0).submit
Do While objIE.Busy: DoEvents: Loop

'请在这里帮忙 '想立即下载 PDF 文件

结束子

【问题讨论】:

    标签: excel vba automation ie-automation


    【解决方案1】:

    我通过循环数月和数年并将它们转换为字符串来构建 URL,然后将其连接到 URL 的基础。我猜你想从哪一年开始,你会在“For year =”声明中看到。

    Sub DownloadFile()
    Dim WinHttpReq As Object
    Dim oStream As Object
    Dim myURL As String
    Dim LocalFilePath As String
    Dim month As String
    Dim year As Integer
    Dim monthNo As Integer
    
    For year = 2010 To 2018
        For monthNo = 1 To 12
            month = MonthName(monthNo)
                myURL = "https://isgs-oas.isgs.illinois.edu/reports/rwservlet?hidden_run_parameters=oil_permit_activity&p_MONTH=" & month & "&p_YEAR=" & CStr(year)
                LocalFilePath = Environ("USERPROFILE") & "\Desktop\rwservlet\oil_permit_activity_" & month & "_" & CStr(year) & ".pdf"
    
                    Set WinHttpReq = CreateObject("Microsoft.XMLHTTP")
                    WinHttpReq.Open "GET", myURL, False, "", ""  '("username", "password")
                    WinHttpReq.send
    
                    If WinHttpReq.Status = 200 Then
                        Set oStream = CreateObject("ADODB.Stream")
                        oStream.Open
                        oStream.Type = 1
                        oStream.Write WinHttpReq.responseBody
                        oStream.SaveToFile LocalFilePath, 2 ' 1 = no overwrite, 2 = overwrite
                        oStream.Close
                    End If
        Next monthNo
    Next year
    End Sub
    

    上面的代码对我有用,但是你必须确保文件夹“rwservlet”存在于你的桌面上,否则它会抛出一个错误(我不擅长错误处理,但我们都在学习)。否则,您可以更改 LocalFilePath 字符串。

    【讨论】:

    • 它在我的系统中也完美运行...非常感谢您的回复。 :)
    【解决方案2】:

    如果您已经知道查询参数的名称,则可以自动构建您的 URL。我使用 Chrome 的检查器找到了上面示例的 URL(“https://isgs-oas.isgs.illinois.edu/reports/rwservlet?hidden_run_parameters=oil_permit_activity&p_MONTH=January&p_YEAR=2018”)。

    您可以重新编写脚本以自动从参数构建 URL,然后使用现有的几个“从 URL 保存文件”脚本之一。这是我在 SE 上找到的一个: Downloading a file in VBA and storing it

    希望对你有帮助:)

    【讨论】:

    • 非常感谢您的回复。但是在这里我遇到了一个问题,URL 没有提到文件类型。因此,每当我从 URL 下载时,文件类型就会变得未知,然后我必须更改文件类型(例如 PDF)。请帮我解决问题,我还想根据年份和县更改名称。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-16
    • 1970-01-01
    • 2022-01-26
    相关资源
    最近更新 更多