Link to home
Start Free TrialLog in
Avatar of rsburge
rsburgeFlag for United States of America

asked on

Save PDF Open in Internet Explorer using VB

I need to save a PDF document that opens in a web browser.  Because of the security on the site, I have to log into the site through the code and then click on the link and have it open in a web browser.  I need to have it as though I were clicking on the save button and entering a file location, but without any user interaction.

I have all of it working, but I can't figure out how to save the PDF without any user interaction.  SendKeys is not an option in this case.

Below is an example that doesn't require the username and password, but can be used for testing.

http://image.email-freedommortgage.com/lib/fefa1577716001/m/2/WR+Wholesale+03-21-12+pc.pdf

Attached is the beginning of the code that logs into the site and navigates to the document I need to save.  Unfortunately, this will just be an example as I can't provide the login information.

Thanks!!

Set ie = CreateObject("internetexplorer.application")
ie.Visible = FALSE
ie.navigate urlMain
    While ie.Busy
        DoEvents
    Wend
    While ie.readyState <> 4
        DoEvents
    Wend
ie.Document.all("userID").Value = un
ie.Document.all("password").Value = pw
ie.Document.Forms("LoginForm").submit
    While ie.Busy
        DoEvents
    Wend
    While ie.readyState <> 4
        DoEvents
    Wend
urlXls = "https://www.freedomwholesale.com/wps/PA_WWolesaleDocuments/DocumentDownloadRequestor?sessionKey=PC_5_QBKMPI42006K5027M6UPHC20O4n17422_documentsList&channel=null&index=0"
ie.navigate urlxls

Open in new window

Avatar of vb_elmar
vb_elmar
Flag of Germany image

Microsoft writes on its website...
http://support.microsoft.com/kb/244757/EN-US/
when downloading a file with WebBrowser control (or Internet Explorer) there's absolutely no way to suppress the prompt asking for saving (or opening) the file.

So additionally to the method ...
CreateObject("internetexplorer.application")
... we have to use the "URLDownloadToFile" method :

Private 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

Private Sub Command2_Click()
myURL = "http://image.email-freedommortgage.com/lib/fefa1577716001/m/2/WR+Wholesale+03-21-12+pc.pdf"
returnValue = URLDownloadToFile(0, myURL, "C:\1.pdf", 0, 0)
MsgBox returnValue = False 'if returnValue = False then SUCCESS!
End Sub
Avatar of rsburge

ASKER

Thank you.

I do use a function similar to this to download many of my files, but it doesn't work with this file because of the security on the site.  I will try this method and see if it works.

Thanks!
Avatar of rsburge

ASKER

Hi - unfortunately, this isn't working with the security on this site.  The resulting file throws an error when opening it stating I need to go to the site and download or save it from there.

Do I have any other options for saving this PDF file besides using send keys?

Can I force this to open in Acrobat rather than in the browser?

Thanks!
ASKER CERTIFIED SOLUTION
Avatar of vb_elmar
vb_elmar
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rsburge

ASKER

Thank you!  I appreciate your help!