Need help regarding IE Automation using VBA

image_1

New Member
Joined
Oct 17, 2010
Messages
9
I have to download a csv file from a website like 50 times a day. I want to automate the process using vba so that i can process it later in excel.
the details are :
website: http://www.nrldc.org/WBS/injsch.aspx
csv file to download labelled by : (ncr)
here is what i have tried but it is not working. need help



Sub Update()
Dim IE As Object
On Error GoTo error_handler
Set IE = CreateObject("InternetExplorer.Application")
With IE
.Navigate "http://www.nrldc.org/WBS/injsch.aspx"
Do While .Busy: DoEvents: Loop
Do While .ReadyState <> 4: DoEvents: Loop
.Visible = True
End With
For Each lnk In objIE.document.Links
If (InStr(lnk, "(ncr)")) Then
'Code to be executed in case it's a good link
objIEPage.Navigate lnk
While objIEPage.Busy
DoEvents
Wend
End If
Next lnk


objIE.Quit
Set IE = Nothing
Exit Sub
error_handler:
MsgBox ("Unexpected Error, I'm quitting.")
objIE.Quit
Set IE = Nothing
End Sub
 
I found the code for which I can take no credit.

You can find the original in this thread http://www.mrexcel.com/forum/showthread.php?t=507871.

I've adapted it for the image_1's URL and incorporated headhair's code.

This code goes in Module1.
Rich (BB code):
'10-Nov-2010
'http://www.mrexcel.com/forum/showthread.php?t=507871
'Note - IE may block the download, displaying its Information Bar at the top of the tab, and preventing this
'program from automatically downloading the file.  To prevent this, add http://www.remotedatacentre.com
'to IE's Trusted sites (Tools - Internet Options - Security - Trusted sites - Sites).

Option Explicit

Public Const baseURL As String = "http://www.remotedatacentre.com/co-so/cbstateall.aspx?dist=Chennai-North"

Public Sub Test_Download()
Dim URL As String
 
    'To open the website
    URL = "http://www.nrldc.org/WBS/injsch.aspx"
    Download_File URL, "", ""
End Sub

Public Sub Download_File(URL As String, saveInFolder As String, saveFilename As String)
    Dim IE As Object
    
  
        Set IE = CreateObject("InternetExplorer.Application")

    With IE
        .Visible = True
        .navigate URL
        While .Busy Or .readyState <> 4: DoEvents: Wend
        .navigate "javascript:__doPostBack('downloadncr','')"
        
    End With
    
    File_Download_Click_Save
        
    Save_As_Set_Filename saveInFolder, saveFilename
    
    Save_As_Click_Save
       
    Download_complete_Click_Close
    
    Debug.Print "Finished"
    
End Sub

Private Sub File_Download_Click_Save()
    
    Dim hWnd As Long
    Dim timeout As Date
    
    Debug.Print "File_Download_Click_Save"
    
    'Find the File Download window, waiting a maximum of 30 seconds for it to appear
    
    timeout = Now + TimeValue("00:00:30")
    Do
        hWnd = FindWindow("#32770", "File Download")
        DoEvents
        Sleep 200
    Loop Until hWnd Or Now > timeout
    
    Debug.Print "   File Download window "; Hex(hWnd)
    
    If hWnd Then
        'Find the child Save button
        
        hWnd = FindWindowEx(hWnd, 0, "Button", "&Save")
        Debug.Print "   Save button "; Hex(hWnd)
    End If
    
    If hWnd Then
    
        'Click the Save button
        
        SetForegroundWindow (hWnd)
        Sleep 600  'this sleep is required and 600 miiliseconds seems to be the minimum that works
        SendMessage hWnd, BM_CLICK, 0, 0
    End If
End Sub

Private Sub Test_Save_As_Set_Filename()
    'Test setting the Save As filename.  The Save As window must be displayed before running this
    Dim theFolder As String, theFilename As String
    theFolder = ThisWorkbook.Path & "\"
    theFilename = "test " & Format(Now, "hh_mm_ss") & ".csv"
    Save_As_Set_Filename theFolder, theFilename
End Sub

Private Sub Save_As_Set_Filename(folder As String, filename As String)
    'Populate the 'File name:' edit window in the Save As dialogue with the specified folder and/or filename.
    'If folder = "" a folder path is not prepended and therefore the default save folder is used.
    'If filename = "" the default file name (already populated) is used.
    
    'The Save As window has the following child window hierarchy:
    
    '   "Save As", #32770 Dialog
    '       "FileName2011_11_11_11_00_26", ComboBoxEx32     (default value in combobox)
    '           "", ComboBox
    '               "FileName2011_11_11_11_00_26"", Edit    (default value in combobox's edit box)
    
    Dim hWnd As Long
    Dim timeout As Date
    Dim fullFilename As String
    
    Debug.Print "Save_As_Set_Filename " & folder
    
    'Find the Save As window, waiting a maximum of 10 seconds for it to appear
    
    timeout = Now + TimeValue("00:00:10")
    Do
        hWnd = FindWindow("#32770", "Save As")
        DoEvents
        Sleep 200
    Loop Until hWnd Or Now > timeout
    If hWnd Then
    
        SetForegroundWindow (hWnd)
        
        'Find the child ComboBoxEx32 window
        
        hWnd = FindWindowEx(hWnd, 0, "ComboBoxEx32", vbNullString)
        Debug.Print "   ComboBoxEx32 "; Hex(hWnd)
    End If
    
    If hWnd Then
    
        'Find the child ComboBox window
        
        hWnd = FindWindowEx(hWnd, 0, "ComboBox", "")
        Debug.Print "   ComboBox "; Hex(hWnd)
    End If
                 
    If hWnd Then
        
        SetForegroundWindow (hWnd)
        'Find the child Edit window
        
        hWnd = FindWindowEx(hWnd, 0, "Edit", "")
        Debug.Print "   Edit "; Hex(hWnd)
    End If
    
    If hWnd Then
            
        If filename = "" Then
            'Get default filename (already populated in Edit window)
            filename = Get_Window_Text(hWnd)
        End If
       
        If folder <> "" And Right(folder, 1) <> "\" Then folder = folder & "\"  'if specified, ensure folder ends with \
        
        fullFilename = folder & filename
        Debug.Print "Full filename " & fullFilename
        
        'Populate the Edit window with the full file name
        
        Sleep 200
        SendMessageByString hWnd, WM_SETTEXT, Len(fullFilename), fullFilename
    End If
    
End Sub

Private Function Get_Window_Text(hWnd As Long) As String
    'Returns the text in the specified window
    
    Dim buffer As String
    Dim length As Long
    Dim result As Long
    
    length = SendMessage(hWnd, WM_GETTEXTLENGTH, 0, 0)
    buffer = Space(length + 1)  '+1 for the null terminator
    result = SendMessage(hWnd, WM_GETTEXT, Len(buffer), ByVal buffer)
    Debug.Print "Edit File name = " & Left(buffer, length)
    Debug.Print "     length = " & length
    
    Get_Window_Text = Left(buffer, length)
    
End Function

Private Sub Save_As_Click_Save()
    'Click the Save button in the Save As dialogue
    
    Dim hWnd As Long
    Dim timeout As Date
    
    Debug.Print "Save_As_Click_Save"
    'Find the Save As window, waiting a maximum of 10 seconds for it to appear
    
    timeout = Now + TimeValue("00:00:10")
    Do
        hWnd = FindWindow(vbNullString, "Save As")
        DoEvents
        Sleep 200
    Loop Until hWnd Or Now > timeout
    If hWnd Then
    
        SetForegroundWindow (hWnd)
            
        'Get the child Save button
        
        hWnd = FindWindowEx(hWnd, 0, "Button", "&Save")
        Debug.Print "   Save button "; hWnd
    End If
    
    If hWnd Then
        'Click the Save button
        
        SendMessage hWnd, BM_CLICK, 0, 0

    End If
        
End Sub

Private Sub Download_complete_Click_Close()
    
    Dim hWnd As Long
    Dim timeout As Date
    
    Debug.Print "Download_complete_Click_Close"
        
    'Find the Download complete window, waiting a maximum of 30 seconds for it to appear.  Timeout value is dependent on the
    'size of the download, so make it longer for bigger files
    
    timeout = Now + TimeValue("00:00:30")

    Do
        hWnd = FindWindow("#32770", "Download complete")
        DoEvents
        Sleep 200
    Loop Until hWnd Or Now > timeout
 
    Debug.Print "   Download complete window "; Hex(hWnd)
    
    If hWnd Then
        'Find the child Close button
        
        hWnd = FindWindowEx(hWnd, 0, "Button", "Close")
        Debug.Print "   Close button "; Hex(hWnd)
    End If
    
    If hWnd Then
    
        'Click the Close button
        
        SetForegroundWindow (hWnd)

        Sleep 600  'this sleep is required and 600 miiliseconds seems to be the minimum that works

        SendMessage hWnd, BM_CLICK, 0, 0

    End If
 
End Sub

This goes in Module2:
Rich (BB code):
Option Explicit
Public Declare Sub Sleep Lib "kernel32" _
    (ByVal dwMilliseconds As Long)
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
    (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" _
    (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Public Declare Function SetForegroundWindow Lib "user32" _
    (ByVal hWnd As Long) As Long
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
    (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Public Declare Function SendMessageByString Lib "user32" Alias "SendMessageA" _
    (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long
Public Const BM_CLICK = &HF5
Public Const WM_SETTEXT = &HC
Public Const WM_GETTEXT = &HD
Public Const WM_GETTEXTLENGTH = &HE
Note where the code goes is important but the actual names of the modules aren't, as long as the 2 sets of code go in separate modules.:)
 
Upvote 0

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
@ norie

tnx for the code you provided. It did exactly what was needed. The main problem as you correctly mentioned in your previous post was dealing with download file dialog box, this solves most of the problem.

But the code doesn't handle the 'file already exists' window if it pops up. If it is possible to add to the code so that it replaces the file if "file already exists" window pops up, it would be perfect.

However this code will also work as i will insert a command in the very start to delete the file in the specified folder so as to ensure that the folder doesnot contain the file when the code tries to save it.

thanks again
 
Upvote 0
@ norie

I tried the code with saving the file to a different folder but its not working. I tried to change the folder path as mentioned in the original post but its not working.

I think its because i am using IE 8 and the code was actually written for a previous version. Need your help in sorting it out.

Currently in its original form it is storing the file in default download folder i.e. the folder in which the last file was saved. That wont help as i have to use the data for further processing. So if it possible have a look.
 
Upvote 0
Sorry, it's not my code.

I know how the basics work but not some of it, especially the filename stuff.

I've only ever tried it once or twice.

Have you definitely specified a path and filename?

If you don't then as far as I can work out the code will save with the default name to the current directory.

Like I said it's not my code, and I've not really had a proper look at it.

One thing I did kind of wonder about was the scope of some of the variables, and that could affect specifying the filename and path.

By the way I'm pretty sure it's not an IE8 problem - that's what I use and the code that actually deals with the dialog box is Windows API.

PS You can check if a file exists by using Dir, and you can change the current directory using ChDir.:)
 
Upvote 0
image_1,

I wrote that code. It was developed with IE8 so should work for you.

To save to a folder other than IE's default download folder, call the Download_File subroutine like this:

Download_File URL, "C:\my\folder\path\", ""

make sure you have the trailing \ in the folder path, because the full filename (saveInFolder & saveFilename) must be valid.

As for the '< file> already exists. Do you want to replace it?' window, I'll write some code to handle that when I have time.
 
Upvote 0
One thing I did kind of wonder about was the scope of some of the variables, and that could affect specifying the filename and path.
Which variables? AFAICS all variables have local scope only, i.e. declared at procedure level, and passed as parameters where required.

PS the code doesn't have to go in separate modules; I just prefer to keep the Windows API declarations separate.
 
Upvote 0
John

Like I said I've only had a look a couple of times - I could be wrong.

I think I might have called things in the wrong order or something - I did change things a little for the URL/page wanted to work with.

The code works fine and it's something that a lot of people have been looking for some time.:)
 
Upvote 0
john,

I tried to run the subroutine as specified by you but it is saving the file in the default directory only. I m using Windows Vista + IE 8.

I m a newbie but i was trying to go through the code. I have a few points in mind:

1. "Test_Save_As_Set_Filename()" is never called then whats the use of it.

2. In "Save_As_Set_Filename(folder As String, filename As String)" the code is trying to find the "child edit window" which I could not understand as when I save the file in IE I get a "Save as" dialog box with a save button. Then after clicking on the save button i get a window in which we can directly enter the fullfilename (folder & filename). So I could not understand the role of "child edit window"

This is the hierarchy in the code:
Code:
hWnd = FindWindow("#32770", "Save As")
hWnd = FindWindowEx(hWnd, 0, "ComboBoxEx32", vbNullString)
hWnd = FindWindowEx(hWnd, 0, "ComboBox", "")
hWnd = FindWindowEx(hWnd, 0, "Edit", "")

I am pretty sure that this will definitely have some significance otherwise you wouldn't have included it but I hope my doubt also make some sense.
 
Upvote 0

Forum statistics

Threads
1,224,814
Messages
6,181,128
Members
453,021
Latest member
Justyna P

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