Solved

Using the FileDownload Event of the IE object

Posted on 2004-10-12
1
692 Views
Last Modified: 2013-12-26
I am currently using a combination of delays and error checks to handle the variable delay between the submission of data to a form and the availability of the File Download dialog box in an automated instance of Internet Explorer. If possible, I would like to replace the delays between the ".Click" and AppActivate"FileDownload" statements in the code fragment shown below by using the FileDownload Event. Can the trial-and-error method shown below be replaced by using the FileDownload Event to indicate the availability of the download dialog window prior to executing the AppActivate statement, and if so, how should it be done?

Thanks

------------code fragment begins------------------------

Ie.Document.All.Item(400).Click            'submit form data for file download

'short timer, less than one second pause to allow filedownload window to load
tdelay = 5   ' equivalent to .5 seconds
tstart = Timer
Do While 10 * (Timer - tstart) < tdelay
    DoEvents    ' Yield to other processes.
Loop

On Error Resume Next                    
AppActivate "File Download"            'first file download window access attempt

If Err.Number <> 0 Then                           'additional  download window access attempts at 1 second intervals
dcount = 0
Do Until Err.Number = 0 Or dcount = 5      'timeout after success or 5 failures
Err.Number = 0
t = DateAdd("s", 1, Now)
Do Until Now > t
    DoEvents
Loop
On Error Resume Next
AppActivate "File Download"
dcount = dcount + 1
Loop
End If


------------code fragment ends------------------------
0
Comment
Question by:mdmat
1 Comment
 
LVL 17

Accepted Solution

by:
zzzzzooc earned 500 total points
ID: 12295699
FileDownload() event is raised prior to the window being shown so you still need to wait anyways. The delays will very greatly.. mainly due to the speeds of different computers.

Here's another method. It covers the main scenario.


Form1:
-----------------
Option Explicit

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private 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
Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long

Private Const BM_CLICK = &HF5
Private Const GW_HWNDFIRST = 0
Private Const GW_HWNDNEXT = 2
Private Const WM_GETTEXT = &HD
Private Const WM_GETTEXTLENGTH = &HE
Private Sub Form_Load()
    Dim lngHandle As Long, lngTemp As Long
    'just browse to test site and wait for it to load...
    Call WebBrowser1.Navigate("http://www.mirc.com/get.html")
    Do Until WebBrowser1.ReadyState = READYSTATE_COMPLETE
        DoEvents
    Loop
    'click on link to download...
    WebBrowser1.Document.All.tags("a")(18).Click
    'wait for "File Download" window to appear...
    Do Until lngHandle <> 0
        lngHandle = FindWindow(vbNullString, "File Download")
        DoEvents
    Loop
    'find the 2nd "File Download" window (the one with "Save")...
    lngHandle = GetWindow(lngHandle, GW_HWNDFIRST)
    Do Until lngHandle = 0
        lngHandle = GetWindow(lngHandle, GW_HWNDNEXT)
        If GetWindowText(lngHandle) = "File Download" Then
            If FindWindowEx(lngHandle, 0, vbNullString, "&Save") <> 0 Then
                lngHandle = FindWindowEx(lngHandle, 0, vbNullString, "&Save")
                Exit Do
            End If
        End If
        DoEvents
    Loop
    If lngHandle <> 0 Then
        'click on "Save"...
        Call SendMessage(lngHandle, BM_CLICK, 0, 0)
        'wait for "Save As" window to show...
        Do
            lngHandle = FindWindow(vbNullString, "Save As")
            DoEvents
        Loop Until lngHandle <> 0
        If lngHandle <> 0 Then
            'ok.. that's the main part.. I'll just use a quick way to
            'finish up
            Call SetForegroundWindow(lngHandle)
            Call SendKeys("c:\test.exe", 1000)
            Call SendKeys("{ENTER}", 1000)
        End If
    End If
End Sub
Private Function GetWindowText(ByVal lngHandle As Long) As String
    Dim strBuff As String, lngLen As Long
    lngLen = SendMessage(lngHandle, WM_GETTEXTLENGTH, 0, 0) + 1
    strBuff = Space(lngLen)
    Call SendMessage(lngHandle, WM_GETTEXT, ByVal lngLen, ByVal strBuff)
    GetWindowText = Left(strBuff, lngLen - 1)
End Function
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now