Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Using the FileDownload Event of the IE object

Posted on 2004-10-12
1
696 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
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…

840 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