Automate File Download from Internet

– Selva V Pasupathy, HSBC Global Resourcing, Hyderabad

The following code illustrates how one can use visual basic for automated file download when the code actually initializes the internetexplorer and navigates the path, and it saves at the location suggested by the user the file that is to be downloaded… Please be advised this code is not well tested and might not be perfect in all scenario.

HOW SHOULD I USE THE FOLLOWING CODE

Option Explicit
Dim OBJIE As InternetExplorer
Dim myurl As String
Dim myExcelFile  As String
'____________________________________________________________
Sub DownLoad_File()
'   --------- Initialize strings like name of the file & -
'   url to navigate --------------------------------------
    
    myExcelFile = FileNameWithPath
    myurl = "http://www.sockofiles.350.com/" & _
            "Form=_With=_no=_title=_bar.xls"
'   ---------- Open internetexplorer and navigate --------
    Set OBJIE = New InternetExplorer
    With OBJIE
        .Navigate myurl
        .AddressBar = 0
        .FullScreen = False
        .Height = 300
        .Width = 400
        .Top = 0
        .Left = 0
        .Toolbar = 0
        .MenuBar = 0
        .Resizable = 0
        .StatusBar = 0
        .Visible = True
    End With
'   --------Wait till IE is free -------------------------
    Do Until OBJIE.ReadyState
        DoEvents
    Loop
'   --------Pass on keys to download and save ------------
    Application.Wait Now() + TimeValue("00:0:03")
    SendKeys ("%s")
    Application.Wait Now() + TimeValue("00:0:03")
    
    SendKeys (myExcelFile)
    Application.Wait Now() + TimeValue("00:0:03")
    SendKeys ("%s")
    Application.Wait Now() + TimeValue("00:0:03")
    OBJIE.Quit
    MsgBox "DONE"
End Sub
'____________________________________________________________
Function FileNameWithPath() As String
    FileNameWithPath = _
        InputBox("Give the complete name of the " & _
        "file that you want to save including path")
End Function

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: