使用VBA将IE报告导出到excel,其中存在导出下拉菜单

使用VBA将IE报告导出到excel,其中存在导出下拉菜单,vba,excel,internet-explorer,export-to-excel,Vba,Excel,Internet Explorer,Export To Excel,我正在使用vba从报表服务器中提取特定于日期的报表。通过在URL中输入日期参数,我可以绕过日期选择器。下一步是将报告导出到excel。它有一个下拉列表来选择文件类型。如何获取从下拉列表中选择的代码?我已经尝试了下面代码的许多变体 Sub Report() Dim ie As Object Application.ScreenUpdating = False Set ie = CreateObject("Internetexplorer.Application")

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

    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"

您需要浏览下拉选项,并更改您要查找的特定选择的
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
选项显式
次级报告()
Dim ie作为对象,oSelection作为对象,i作为长
Application.ScreenUpdating=False
设置ie=CreateObject(“Internetexplorer.Application”)
如果ie为空,则退出Sub
与ie
.Visible=True
.导航“http://reportserver.comp.local/ReportServer/Pages/ReportViewer.aspx?%2fOperations%2fAR%2fCash+收据和开始日期=2017-01-07和结束日期=2017-01-13“

“我猜这是正确的id检查页源代码“Excel选项是第一个选项。我有点困惑我是否使用了正确的ID,因为有一个用于表格,一个用于按钮,一个用于按钮图像,一个用于下拉菜单…我已经尝试了所有这些,但没有任何效果。我试着看看我是否可以用:Format=EXCELOPENXML这样的语言编写,但我想这并不容易。如果你有FireFox,选择区域或选择下拉框,查看源代码,你应该能够找到它。如果您使用IE,请在
处设置断点。退出
,运行代码,如果未更改为所需选项,请将黄色箭头移动到
If not oSelection…
,然后在即时窗口
Set oSelection=.document.getelementbyid(“下一个测试ID”)
中将ID更改为找到的ID之一,如果所需ID发生更改,这就是你需要的。