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.

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.


Set ie = CreateObject("internetexplorer.application")
ie.Visible = FALSE
ie.navigate urlMain
    While ie.Busy
    While ie.readyState <> 4
ie.Document.all("userID").Value = un
ie.Document.all("password").Value = pw
    While ie.Busy
    While ie.readyState <> 4
urlXls = ""
ie.navigate urlxls

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Microsoft writes on its website...
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 ...
... 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 = ""
returnValue = URLDownloadToFile(0, myURL, "C:\1.pdf", 0, 0)
MsgBox returnValue = False 'if returnValue = False then SUCCESS!
End Sub
rsburgeAuthor Commented:
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.

rsburgeAuthor Commented:
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?

If you can uncheck the checkbox...
"always ask before opening this type of file"
 (see attached image)
...the "File Download" Dialog would not pop up.

Then you could use the command...
ie.ExecWB 4, 2   'SAVE  FILE  AS  
ie.ExecWB 6, 2   'PRINT  the  File       SAVE or PRINT the file.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rsburgeAuthor Commented:
Thank you!  I appreciate your help!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.