Solved

Using the FileDownload Event of the IE object

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

747 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

12 Experts available now in Live!

Get 1:1 Help Now