【问题标题】:Excel VBA - Exporting and saving SSRS report [duplicate]Excel VBA - 导出和保存 SSRS 报告 [重复]
【发布时间】:2018-06-13 09:27:19
【问题描述】:

在使用 VBA 从公司内部网站提取 SSRS 报告方面寻求帮助。我已经阅读了几篇关于 VBA 自动化的帖子,并且成功地做出了所有选择并运行了报告,现在我只需要数据。

具体来说,我需要帮助来选择导出到 excel 选项。

我当前的 VBA 代码:

Sub macro_07()
Dim appIE As InternetExplorerMedium
'Set appIE = Nothing
Dim objElement As Object
Dim objCollection As Object
Dim objCollection2 As Object
Dim objCollection3 As Object
Dim objCollection4 As Object
Dim objCollection5 As Object
Dim objCollection6 As Object
Dim objCollection7 As Object
Dim objCollection8 As Object

Set appIE = New InternetExplorerMedium
sURL = ThisWorkbook.Sheets("Control").Range("IALinks").Cells(1, 1).Value
With appIE
    .Navigate sURL
    .Visible = True
End With

Do While appIE.Busy Or appIE.readyState <> 4
    DoEvents
Loop

Set objCollection = appIE.document.getElementById("ctl31_ctl04_ctl07_txtValue")
objCollection.Value = Range("YearSlct").Value
Set objCollection2 = appIE.document.getElementById("ctl31_ctl04_ctl03_ddValue")
objCollection2.selectedIndex = 4
objCollection2.FireEvent "onchange"
Application.Wait (Now + TimeValue("0:00:002"))
Set objCollection3 = appIE.document.getElementById("ctl31_ctl04_ctl05")
objCollection3.Click
Set objCollection4 = appIE.document.getElementById("ctl31_ctl04_ctl05_divDropDown_ctl01")
objCollection4.Focus
objCollection4.Checked = "checked"
Set objCollection5 = appIE.document.getElementById("ctl31_ctl04_ctl05_divDropDown_ctl03")
objCollection5.Focus
objCollection5.Checked = "checked"
objCollection5.FireEvent "onchange"

Set objCollection6 = appIE.document.getElementById("ctl31_ctl04_ctl00")
objCollection6.Click
Application.Wait (Now + TimeValue("0:00:002"))

Set objCollection7 = appIE.document.getElementById("ctl31_ctl06_ctl04_ctl00_Button")
objCollection7.Click


  Set appIE = Nothing
End Sub

这让我选择了导出按钮,但我不知道如何选择 excel 选项。

导出菜单:

这是网站的 HTML 代码:

<table style="display:inline;" cellspacing="0" cellpadding="0">
                        <tbody><tr>
                            <td height="28"><div class="HoverButton" id="ctl31_ctl06_ctl04_ctl00">
                                <table title="Export" id="ctl31_ctl06_ctl04_ctl00_Button" border="0">
                                    <tbody><tr>
                                        <td><a id="ctl31_ctl06_ctl04_ctl00_ButtonLink" style="cursor: pointer;" href="javascript:void(0)"><img id="ctl31_ctl06_ctl04_ctl00_ButtonImg" style="border-style:None;height:16px;width:16px;border-width:0px;" src="/Reports/Reserved.ReportViewerWebControl.axd?OpType=Resource&amp;Version=10.50.1600.1&amp;Name=Microsoft.Reporting.WebForms.Icons.Export.gif"><img id="ctl31_ctl06_ctl04_ctl00_ButtonImgDown" style="border-style:None;height:6px;width:7px;border-width:0px;margin-bottom:5px;margin-left:5px;" src="/Reports/Reserved.ReportViewerWebControl.axd?OpType=Resource&amp;Version=10.50.1600.1&amp;Name=Microsoft.Reporting.WebForms.Icons.ArrowDown.gif"></a></td>
                                    </tr>
                                </tbody></table>
                            </div><div class="MenuBarBkGnd" id="ctl31_ctl06_ctl04_ctl00_Menu" style="padding: 1px; display: block; visibility: visible; position: absolute;">
                                <div class="HoverButton">
                                    <a class="ActiveLink" style="padding: 3px 8px 3px 32px; text-decoration: none; display: block; white-space: nowrap;" onclick="$find('ctl31').exportReport('XML');" href="javascript:void(0)">XML file with report data</a>
                                </div><div class="DisabledButton">
                                    <a class="ActiveLink" style="padding: 3px 8px 3px 32px; text-decoration: none; display: block; white-space: nowrap;" onclick="$find('ctl31').exportReport('CSV');" href="javascript:void(0)">CSV (comma delimited)</a>
                                </div><div class="DisabledButton">
                                    <a class="ActiveLink" style="padding: 3px 8px 3px 32px; text-decoration: none; display: block; white-space: nowrap;" onclick="$find('ctl31').exportReport('PDF');" href="javascript:void(0)">PDF</a>
                                </div><div class="DisabledButton">
                                    <a class="ActiveLink" style="padding: 3px 8px 3px 32px; text-decoration: none; display: block; white-space: nowrap;" onclick="$find('ctl31').exportReport('MHTML');" href="javascript:void(0)">MHTML (web archive)</a>
                                </div><div class="DisabledButton">
                                    <a class="ActiveLink" style="padding: 3px 8px 3px 32px; text-decoration: none; display: block; white-space: nowrap;" onclick="$find('ctl31').exportReport('EXCEL');" href="javascript:void(0)">Excel</a>
                                </div><div class="DisabledButton">
                                    <a class="ActiveLink" style="padding: 3px 8px 3px 32px; text-decoration: none; display: block; white-space: nowrap;" onclick="$find('ctl31').exportReport('IMAGE');" href="javascript:void(0)">TIFF file</a>
                                </div><div class="DisabledButton">
                                    <a class="ActiveLink" style="padding: 3px 8px 3px 32px; text-decoration: none; display: block; white-space: nowrap;" onclick="$find('ctl31').exportReport('WORD');" href="javascript:void(0)">Word</a>
                                </div>
                            <div style="left: 0px; top: 0px; width: 26px; height: 149px; filter: none; position: absolute; z-index: -1; opacity: 0.05; background-color: black;"></div></div></td>
                        </tr>
                    </tbody></table>

【问题讨论】:

  • 因为您是 Stack Overflow 的新手,所以提示:每个问题只问一个问题,否则可能会因为“太宽泛”而被关闭。您可能想花点时间阅读如何在help center 的网站上有效地提问。您可以使用edit 链接修改问题。
  • 建立ActiveLink类的集合,然后迭代找到以Excel为内文的类并单击它。
  • 谢谢瑞恩。我不知道该怎么做,但在您之前回答的问题中找到了答案。

标签: vba excel reporting-services ie-automation


【解决方案1】:

设置报表订阅,报表服务器将为您导出 Excel 文件到共享文件。

或者,您可能想要调查报表背后的数据库查询并(重新)将报表创建到 Excel 中。

https://docs.microsoft.com/en-us/sql/reporting-services/subscriptions/subscriptions-and-delivery-reporting-services?view=sql-server-2017

【讨论】:

  • 感谢您的意见。不幸的是,我只能通过浏览器访问拉取报告,这就是我尝试使用 IE 自动化的原因。我会就这些选项与 IT 团队核实,但我正在寻找可以通过我当前的访问权限完成的任务。
  • 另一个“简单”选项(如果允许 Excel 从网页导入数据)是打开 Excel,新工作簿,单击数据选项卡,选择“来自 Web”并粘贴到报告 URL并按照向导在掌握了如何执行这些步骤后,尝试使用上述步骤录制宏。
  • 谢谢。那是我首先开始的地方。我对您建议的订阅解决方案有所了解。
【解决方案2】:

感谢大家的留言。我能够按照 Ryan 的建议找到解决方案,并查看他在之前的帖子中提供的答案 (Get all innertext VBA)

从那里开始是一个简单的循环:

    For Each Element In Elements
        If Element.innerText = "Excel" Then Element.Click
    Next

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多