We help IT Professionals succeed at work.
Get Started

PDF download from Web via VBA - going through a security page

1,762 Views
Last Modified: 2013-11-27
I am trying to download a page from the web and have to go through two security pages before getting to the PDF file.  The attached code is getting me through the security but my last part is apparantly opening a new browser windows before attempting to download the pdf.  I need it to use the open window and download the pdf.

The function (URLDownloadToFile) called in the code is :
Public Declare Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" (ByVal pCaller As Long, ByVal szURL As String, ByVal szFileName As String, ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long

I have set references to InetTransferLib and Microsoft Internet Controls.

Any assistance would be greatly appreciated.
Dim ieApp As InternetExplorer
    Dim iePage As HTMLDocument
    Dim x As Single
    Dim timestart As String
    Const cTIME = 10000 'in MilliSeconds
    
    Set ieApp = New InternetExplorer
    
    ieApp.Visible = True
    
    'This changes based upon the document needed, hardcoded here for the example
    ieApp.Navigate "https://ecf.ganb.uscourts.gov/cgi-bin/show_case_doc?20,729812,,14604849,"
    
    'wait for page to load
    Do Until ieApp.ReadyState = READYSTATE_COMPLETE
    Loop
    
    Set iePage = ieApp.Document
    
    'Enter the User information and password in the first web page
    iePage.Forms(0).Item("login").Value = "cw0133"
    iePage.Forms(0).Item("key").Value = "4scirvy2"
    iePage.Forms(0).Item("clcode").Value = "system"
    iePage.Forms(0).Item("button1").Click
    
    'Wait for the second page to load and hit the submit button
    Call sSleep(cTIME)
    ieApp.Document.Forms(0).submit
    
    'Wait for the PDF file to load
    Call sSleep(cTIME)
        
    '***********************************************************************************************
    'This is my problem area it opens a new browser instead of using the one that has gone through the security check
    'Download the PDF file  - hardcoded url and file name for this example
    If URLDownloadToFile(0&, "https://ecf.ganb.uscourts.gov/cgi-bin/show_case_doc?20,729812,,14604849,", "c:\TEMP\TEST110908.PDF", 0&, 0&) = 0 Then
        MsgBox "Downloaded"
    Else
        MsgBox "Failed"
    End If
    '***********************************************************************************************
 
 
 
    ' Kill the browser
    Set ieBrowser = Nothing

Open in new window

Comment
Watch Question
This problem has been solved!
Unlock 3 Answers and 11 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE