1

I have a code which is automating Internet Explorer, with IE visibility set to false so that the code operates in the background. My code clicks an element ID which opens a separate IE window which is visible and contains a PDF report. Unfortunately, I have been unable to get the new IE window to open but remain invisible and download a copy of the PDF to the computer desktop.

I have tried navigating directly to the link of the newly opened page with the report however the link can vary.

I am clicking element "btnContinue" on the site to open the report:

<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">

Here is my code:

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

I need this code to open the new page, remain invisible, and download the PDF to the desktop OR somehow trigger the report to download from the onclick (if possible) without needing to open the new page from clicking the element. I have tried to navigate/trigger the javascript onclick event to see if the report can be downloaded directly, without clicking the element to open the report in a new window but unfortunately not having any success. Any help would be greatly appreciated.

jpmtozer
  • 45
  • 6
  • 1
    If you know or can extract the URL, then you should be able to use the URLDownloadToFile function. https://stackoverflow.com/questions/42419486/how-to-download-a-file-from-sharepoint-with-vba/42422964#42422964 – Tragamor Sep 13 '19 at 13:54
  • Hi Trag, thank you for your response. The issue with that is that the URL from the newly opened page can differ depending on the date the report is requested, amongst some other factors. So the URL will vary and not be a constant. – jpmtozer Sep 13 '19 at 14:19
  • Have you tried calling javascript directly? Not sure if that would make any difference but worth a try: `Call IE.Document.parentWindow.execScript(js, "JavaScript")` where `js` is variable. I hope you find a way. – Daniel Sep 13 '19 at 15:54
  • Hi @Daniel thanks for the response, apologies i am a little bit out of my depth here with the javascript, can you advise how this should fit in with my code? I tried the below but got a debug notice. Also apologies but I have followed the formatting guide and cannot get this code to format properly in the comments. 'Call ie.document.parentWindow.execScript(js, "javascript:pop('Splash.aspx?SplashID=3&../Reports/View.aspx&ReporttypeID=1&lang=EN&ruletypeid=5&ruleid=0'")' – jpmtozer Sep 13 '19 at 16:48
  • Do you see any patterns in the URL which you could recreate? It's often the case that certain parameters are included into the URL. The values of those parameters (eg. date) may change depending on the report you requested but the structure of the URL remains the same. Maybe if you create a string consisted of the parts of the URL that are always the same and combine it with string variables that will hold the value of the different parameters, you can avoid using IE altogether. – Stavros Jon Sep 14 '19 at 08:08
  • @jpmtozer the `js` (first argument) should be a script name, the second argument should remain unchanged "JavaScript" – Daniel Sep 16 '19 at 08:33

0 Answers0