VBA - Save a file from a Website

giffordj

New Member
Joined
Nov 7, 2003
Messages
20
I ran into a problem with the following code

Dim URL As String
URL = Worksheets("References & Resources").Range("URLMSL")
Dim IE As Object
Set IE = CreateObject("internetexplorer.application")
IE.Navigate URL
IE.Visible = True

My problem is that on most of the workstations here, it will open internet explorer, then open the file in Excel. I just want it to Save the file. I know there is probably another way to do this that will satisfy my need.
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Hi, not sure what your asking. I tried your code and it opens an IE window just fine. Are you saying workbook is closed when its run? Which file to save are you talking about. :biggrin:
 
Upvote 0
Hi giffordj,
If you just want to download a file from the net, you don't have to use IE.
Please try something like this with API named "URLDownloadToFileA".

Code:
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
  
Sub DownloadFilefromWeb()
    Dim strSavePath As String
    Dim URL As String, ext As String
    Dim buf, ret As Long
    URL = Worksheets("References & Resources").Range("URLMSL").Value
    buf = Split(URL, ".")
    ext = buf(UBound(buf))
    strSavePath = ThisWorkbook.Path & "\" & "DownloadedFile." & ext
    ret = URLDownloadToFile(0, URL, strSavePath, 0, 0)
    If ret = 0 Then
        MsgBox "Download has been succeed!"
    Else
        MsgBox "Error"
    End If
End Sub
 
Upvote 0
The problem with my code, is if the user has IE set to open excel once the file is downloaded, then they have to save the file. The goal of my little project is no user interaction at all.

Colo your code gave me an error. I believe it is caused because it needs to autheticate to the server that has the file on it. I would like to get it work, so any suggestions to fix it will be welcomed.
 
Upvote 0
This here seems to work. Do a search on URLDownloadToFile in Google if it doesnt work for you.

hth
 
Last edited by a moderator:
Upvote 0
Hi giffordj, :)

To handle IE, You can use the ExecWB command. Regrding to this, please see the links below.

Code:
ExecWB(cmdID As OLECMDID, cmdexecopt As OLECMDEXECOPT, [pvaIn], [pvaOut])

http://msdn.microsoft.com/library/d...owser/webbrowser/reference/methods/execwb.asp


The code would be something like this...

Code:
Sub TESTING()
'Need to reference to Microsoft Internet Controls
    Dim URL As String
    'URL = Worksheets("References & Resources").Range("URLMSL")
    URL = "http://www.mrexcel.com/board2/viewtopic.php?t=69685" 'for TEST
    Dim IE As Object
    Set IE = CreateObject("internetexplorer.application")
    IE.Visible = True
    IE.Navigate URL
    Do While IE.ReadyState <> 4
        DoEvents
    Loop
    SendKeys "%S"
    IE.ExecWB OLECMDID_SAVEAS, OLECMDEXECOPT_DODEFAULT    'SaveAs
End Sub
 
Upvote 0
Hi giffordj, :)

To handle IE, You can use the ExecWB command. Regrding to this, please see the links below.

Code:
ExecWB(cmdID As OLECMDID, cmdexecopt As OLECMDEXECOPT, [pvaIn], [pvaOut])

http://msdn.microsoft.com/library/d...owser/webbrowser/reference/methods/execwb.asp


The code would be something like this...

Code:
Sub TESTING()
'Need to reference to Microsoft Internet Controls
    Dim URL As String
    'URL = Worksheets("References & Resources").Range("URLMSL")
    URL = "http://www.mrexcel.com/board2/viewtopic.php?t=69685" 'for TEST
    Dim IE As Object
    Set IE = CreateObject("internetexplorer.application")
    IE.Visible = True
    IE.Navigate URL
    Do While IE.ReadyState <> 4
        DoEvents
    Loop
    SendKeys "%S"
    IE.ExecWB OLECMDID_SAVEAS, OLECMDEXECOPT_DODEFAULT    'SaveAs
End Sub

Colo,

This code seems to hang on me.
I am running it like this:
I have asked the orignial question here (crosspost disclosure)
Code:
Public Sub LaunchtheWebGasDay()
 
    Dim objIE As InternetExplorer
    Set objIE = New InternetExplorer 'Create Internet Explorer Object
 
    objIE.Navigate "http://www.transcanada.com/Customer_Express/tools/gdsr_transcanada.htm" 'Navigate the URL
 
    Do Until objIE.ReadyState = READYSTATE_COMPLETE: Loop 'Wait for page to load
            objIE.Document.forms(1).elements(0).Checked = True '"Alberta"
            'objIE.Document.forms(1).elements(1).Checked = True '"Metric"
            'objIE.Document.forms(1).elements(2).Checked = True '"Mainline"
            objIE.Document.forms(1).elements(3).Checked = True '"Imperial"
            objIE.Document.forms(1).elements(4).Value = "20091021"
            objIE.Document.forms(1).elements(6).Value = ".csv"
            objIE.Document.forms(1).elements(7).Click 'Click submit
 
                Do While objIE.ReadyState <> 4
                    DoEvents
                Loop
                SendKeys "%S"
            objIE.ExecWB OLECMDID_SAVEAS, OLECMDEXECOPT_DODEFAULT    'SaveAs
            
Set objIE = Nothing
End Sub
 
Upvote 0
Ran the test code.. where did it download to? :confused: EDIT:: FOUND IT LOL..
Hi giffordj, :)

To handle IE, You can use the ExecWB command. Regrding to this, please see the links below.

Code:
ExecWB(cmdID As OLECMDID, cmdexecopt As OLECMDEXECOPT, [pvaIn], [pvaOut])

http://msdn.microsoft.com/library/d...owser/webbrowser/reference/methods/execwb.asp


The code would be something like this...

Code:
Sub TESTING()
'Need to reference to Microsoft Internet Controls
    Dim URL As String
    'URL = Worksheets("References & Resources").Range("URLMSL")
    URL = "http://www.mrexcel.com/board2/viewtopic.php?t=69685" 'for TEST
    Dim IE As Object
    Set IE = CreateObject("internetexplorer.application")
    IE.Visible = True
    IE.Navigate URL
    Do While IE.ReadyState <> 4
        DoEvents
    Loop
    SendKeys "%S"
    IE.ExecWB OLECMDID_SAVEAS, OLECMDEXECOPT_DODEFAULT    'SaveAs
End Sub
 
Upvote 0

Forum statistics

Threads
1,213,484
Messages
6,113,920
Members
448,533
Latest member
thietbibeboiwasaco

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