使用存在导出下拉菜单的VBA将IE报告导出为Excel

时间:2017-01-12 21:02:10

标签: excel vba excel-vba internet-explorer export-to-excel

我正在使用vba从报表服务器中提取特定日期的报表。我可以通过在URL中输入日期参数​​来绕过日期选择器。下一步是将报告导出为ex​​cel。它有一个下拉列表来选择文件类型。如何从下拉列表中选择代码?我已尝试过以下代码的许多变体。

    Sub Report()
    Dim ie As Object
    Application.ScreenUpdating = False
    Set ie = CreateObject("Internetexplorer.Application")
    ie.Visible = True
    ie.navigate "http://reportserver.comp.local/ReportServer/Pages/ReportViewer.aspx?%2fOperations%2fAR%2fCash+Receipts&StartDate=2017-01-07&EndDate=2017-01-13"
    ie.document.getelementbyid("ReportViewerControl_ctl05_ctl04_ctl00_ButtonImgDown").Click
    ie.document.getelementbyid("ReportViewerControl_ctl05_ctl04_ctl00_Menu").Click
    ie.document.getelementbyid("ReportViewerControl_ctl05_ctl04_ctl00_Button").Value = "Excel"

1 个答案:

答案 0 :(得分:0)

您需要浏览下拉选项并更改您所追踪的特定选择selectedIndex。请尝试以下:

Option Explicit

Sub Report()
    Dim ie As Object, oSelection As Object, i As Long
    Application.ScreenUpdating = False
    Set ie = CreateObject("Internetexplorer.Application")
    If ie Is Nothing Then Exit Sub
    With ie
        .Visible = True
        .navigate "http://reportserver.comp.local/ReportServer/Pages/ReportViewer.aspx?%2fOperations%2fAR%2fCash+Receipts&StartDate=2017-01-07&EndDate=2017-01-13"

        ' I am guessing this is the right id check page source code "<select id='...'"
        Set oSelection = .document.getelementbyid("ReportViewerControl_ctl05_ctl04_ctl00_Menu")

        If Not oSelection Is Nothing Then
            For i = 0 To oSelection.all.Length - 1
                If InStr(1, oSelection.all(i), "Excel", vbTextCompare) > 0 Then
                    oSelection.selectedIndex = i
                    Exit For
                End If
            Next
            Set oSelection = Nothing
        End If
        .Quit
    End With
    Set ie = Nothing
    Application.ScreenUpdating = True
End Sub