首页 > 解决方案 > 如何使用 Javascript onclick 自动化 Excel 从 Internet Explorer 下载 PDF

问题描述

我有一个自动化 Internet Explorer 的代码,将 IE 可见性设置为 false,以便代码在后台运行。我的代码单击一个元素 ID,该 ID 打开一个单独的 IE 窗口,该窗口可见并包含 PDF 报告。不幸的是,我无法打开新的 IE 窗口,但仍然不可见并将 PDF 的副本下载到计算机桌面。

我尝试使用报告直接导航到新打开页面的链接,但是链接可能会有所不同。

我在网站上单击元素“btnContinue”以打开报告:

<input name="btnContinue" class="btn btn-primary" id="btnContinue" onclick="javascript:pop('Splash.aspx?SplashID=3&amp;../Reports/View.aspx&amp;ReporttypeID=1&amp;lang=EN&amp;ruletypeid=5&amp;ruleid=0'); return false;" type="submit" value="Continue">

这是我的代码:

Sub navigate()
Const url = "https://myURL"
Dim ie As Object
Set ie = CreateObject("InternetExplorer.Application")    
With ie 'Open IE
    .navigate url
    ieBusy ie
    .Visible = False
        If Item.ID = "btnNextPage" Then
            Item.Click
            Exit For
        End If
    Next
    ieBusy ie
‘Below code clicks the element ID and opens a new IE with the report – Code needs to open a new IE but visibility remain false and download the newly opened report OR report triggered to be downloaded directly from element/javascript onclick.        
For Each Item In ie.document.all
        If Item.ID = "btnContinue" Then
            Item.Click
            Exit For
        End If
    Next
    ieBusy ie        
End With 'End IE actions
End Sub
Sub ieBusy(ie As Object)
Do While ie.Busy Or ie.ReadyState < 4
    DoEvents
Loop 'Loop
End Sub

我需要此代码来打开新页面,保持不可见,并将 PDF 下载到桌面或以某种方式触发报告从 onclick 下载(如果可能),而无需通过单击元素打开新页面。我试图导航/触发 javascript onclick 事件以查看是否可以直接下载报告,而无需单击元素以在新窗口中打开报告,但不幸的是没有任何成功。任何帮助将不胜感激。

标签: javascriptexcelvbapdf

解决方案


推荐阅读