Download instead of open (display) files using IE

Pegaso

New Member
Joined
Jun 25, 2012
Messages
21
Dear All,


I have a list of URLs, each one links to a document (.doc, .docx, .bmp, .jpg, etc.). The database is not located locally, and the only way to access the stored data is through the previously mentioned URLs.

Each time I open a link, IE displays the document instead of downloading it. How can I change that behavior in IE? I need the files to be downloaded instead of opened. The huge amount of files (>10000) makes it impractical to save each document manually.

A possible solution (I would need help to implement it), would be to use a virtual pdf printer (PDF Creator for instance), to save the file as a .pdf, but this has to be automated as well, due to the high volume of files.

The code I have so far (!!!a million thanks to Vladimir, ZVI; for helping in develop the code that allowed me to download >100000 non-pdf files!!!), works fine with .pdf files but not with the rest:

Code:
'Creates the links to access the files in each vault
'Opens each external link, creates a list with all the files and local links for each one




Sub MainOne() 'Get Vault's URL using IE


Sheets("Summary").Activate


VaultMin = Cells(2, 2)
VaultMax = Cells(2, 4)
PathVault = Cells(3, 6)
u = 0


For t = VaultMin To VaultMax
    
    'Section 1 of 3: Read data, creates vault URL, open, extract HTML code
    
    Cells(2, 3) = t
    NameVault = "HTML-" & t & ".txt"
    Url = Cells(2, 6)
    'Url = "http://google.com" 'testing
    
    Set IE = CreateObject("internetexplorer.application")
    IE.Navigate Url


    'Wait for page to load!
    Do
    If IE.ReadyState = 4 Then
    IE.Visible = False 'True 'False
    Exit Do
    Else


    DoEvents
    End If
    Loop


    IE.Quit
    Set IE = Nothing
       
Next t


End Sub

I can use any other browser if that fixes my problem.


By the way, I also posted a similar thread here:


sevenforums.com/browsers-mail/369669-download-instead-open-display-files.html#post3064546
techsupportforum.com/forums/f56/download-instead-of-open-display-files-using-ie-993874.html#post6300994


Any help is highly appreciated!


Warmest regards


Pegaso
 
Nothing thrilling:



Code:
Private Declare Function DownloadWebFile 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

Sub M_snb()
    DownloadWebFile 0, "http://www.google.com/intl/en/images/logo.gif", "G:\OF\GoogleLogo.gif", 0, 0
End Sub
 
Upvote 0
Nothing thrilling:



Code:
Private Declare Function DownloadWebFile 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

Sub M_snb()
    DownloadWebFile 0, "http://www.google.com/intl/en/images/logo.gif", "G:\OF\GoogleLogo.gif", 0, 0
End Sub

Dear SNB,

Thanks for your suggestion, but this way of requesting the data from the database doesn't work here. I tried the same code in any open webpage and it works, but not in my case, let me explain:

In order to access the data I have to login in a corporate database, that's why I can't give you a full URL, but I'll try to be as specific as possible. Before running the macro I login to the database (I can successfully login using either Chrome or IE), I test pasting the URL I want to download from and it displays the content of the file in the browser, then I use the code above and the the content of the downloaded file is:

Code:
******** ********************>alert('Your Session Has Expired -/new_xxxyyyzzzASP/fileParse.php?attachID=123456&fileID=78910 - Please logout completely and login again.');*********>

So it looks like using this method the database doesn't understand that I am already logged in, I tried the code setting the default browser to IE and Chrome, with similar results.

The files I need to download have this structure: "http://xxxyyyzzz.com/new_aaabbbccc/fileParse.php?attachID=123456&fileID=7891011"
I know in advance the type of file stored (.doc, .txt, .bmp, etc..), so I can add it to the name of the file being downloaded.

Few weeks ago I started dealing with the .pdf files stored in the database, and I was able to download them (>100000 files) after disabling the .pdf extension for Chrome and IE, forcing the files to be downloaded, but I couldn't do something similar for the rest of the non-pdf files.

Since I can open and display the files in the browser, there must be an easy way of saving the content of the browser window to a variable within VBA, I just need to figure out how.

I sincerely appreciate any help with this issue!

Warmest regards!

Pegaso
 
Upvote 0
Since I can open and display the files in the browser, there must be an easy way of saving the content of the browser window to a variable within <acronym title="visual basic for applications" style="border-width: 0px 0px 1px; border-bottom-style: dotted; border-bottom-color: rgb(0, 0, 0); cursor: help; color: rgb(51, 51, 51); background-color: rgb(250, 250, 250);">VBA</acronym>, I just need to figure out how.

This is what I meant:

Code:
Set IE = CreateObject("internetexplorer.application")


    IE.Navigate Url


    'Wait for page to load!
     ...


    Set objDoc = IE.Document
    strMyPage = objDoc.Body.innerHTML


    Set fs = CreateObject("Scripting.FileSystemObject")
    Set f = fs.CreateTextFile(PathVault & NameVault, True)
    
    f.Write Chr(9) & strMyPage
    f.Close

I used that piece of code to extract the html code of a certain URL, using the property (apologies if using the wrong jargon) "objDoc.Body.innerHTML", then there must be a property to refer to the content of the URL, what is it?

Warmest regards,

Pegaso
 
Upvote 0
I just discovered that objDoc.Body.innerText allows me to capture the text in the URL, but what if the URL contains a non-text file (.bmp, .jpg, .zip etc...)?
 
Upvote 0

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top