Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Using the FileDownload Event of the IE object

Posted on 2004-10-12
1
Medium Priority
?
701 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
[X]
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
1 Comment
 
LVL 17

Accepted Solution

by:
zzzzzooc earned 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

636 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