Solved

Using the FileDownload Event of the IE object

Posted on 2004-10-12
1
697 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Windows 10 start screen issues 9 61
to transfer string from C lanaguage to VBA 4 78
Visual Studio search word table and return Cell index 8 78
using web browser with BING 40 136
Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

726 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