Question

"File Download" Automation

Asked by: rsburge

As in the related question, I am trying to automate a File download dialog from a web site, using IE automation in VBA.

The code in the related question is designed to click save and enter a file name and location etc.  I just need to alter the code to simply click the open button.

I've been able to do that (somewhat), but I think there is some extra code in here that I don't need.  Can someone please help me clean this up so that it...

1. waits for the File Download dialog to appear
2. clicks on the open button

Thanks!!

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 SetActiveWindow Lib "user32.dll" (ByVal hwnd As Long) As Long
Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Declare Function SendMessageByString Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long
Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Declare Function GetWindowRect Lib "user32.dll" (ByVal hwnd As Long, lpRect As RECT) As Long
Declare Function SetCursorPos Lib "user32.dll" (ByVal X As Integer, ByVal Y As Integer) As Integer
 
Private Type RECT
        Left As Long
        Top As Long
        Right As Long
        Bottom As Long
End Type
 
Private Const WM_SETTEXT = &HC
Private Const WM_GETTEXT = &HD
Private Const WM_GETTEXTLENGTH = &HE
Private Const BM_CLICK = &HF5
 
 
 
Sub FileDwnld()   
 
Set xlapp = CreateObject("excel.application")
xlapp.Visible = False
Dim hwnd As Long, HWND1 As Long, HWND2 As Long, HWND3 As Long, HWND4 As Long, HWND5 As Long, HWNDCont As Long, HWNDCont1 As Long
Dim Textlen As Long, FSO As Object, Portfolio As Object
Dim Texte As String, Txt As String, I As Integer, J As Integer, K As Integer
Dim tRECT As RECT
 
 
 
        HWNDCont = 9999
         
        While HWNDCont <> 0
         
        For I = 1 To 12
         
            hwnd = FindWindow("#32770", "File Download")
            If hwnd > 0 Then
                For J = 1 To 30
                    HndB = FindWindowEx(hwnd, 0, "Button", "&Open")
                    If HndB > 0 Then
                        
                        GetWindowRect HndB, tRECT
         
                        With tRECT
                            X = .Left + (.Right - .Left) / 2
                            Y = .Top + (.Bottom - .Top) / 2
                        End With
                        
                        SetForegroundWindow hwnd
                        
                        SetCursorPos X, Y
                        'Delay 0.3
                        DoEvents
                        
                        SendMessage HndB, BM_CLICK, 0, 0
                        Exit For
                    Else
                        'Delay 0.2
                    End If
                    Debug.Print "J", J
                Next J
                Exit For
                
            Else
                'Delay 0.5
            End If
            Debug.Print "I", I
        Next I
         
        For K = 1 To 5
            hwnd = 0
            HWND1 = 0
            HWND2 = 0
            HWND3 = 0
            HWND4 = 0
            HWND5 = 0
            
            hwnd = FindWindow("#32770", "File Download")
            If hwnd > 0 And HWNDCont1 <> hwnd Then
                    HndB = FindWindowEx(hwnd, 0, "Button", "&Open")
                    HWND1 = FindWindowEx(hwnd, 0, "DUIViewWndClassName", vbNullString)
                    HWND2 = FindWindowEx(HWND1, 0, "DirectUIHWND", vbNullString)
                    HWND3 = FindWindowEx(HWND2, 0, "FloatNotifySink", vbNullString)
                    HWND4 = FindWindowEx(HWND3, 0, "Combobox", vbNullString)
                    HWND5 = FindWindowEx(HWND4, 0, "Edit", vbNullString)
         
                'filePath = "G:\aLXE-Pricing\provident\provident.csv"
                'Txt = filePath
         
                
                'deletes it if it already exists
                'If Len(Dir(Txt)) > 0 Then Kill Txt
                
                'Returns the new name and path
                'SendMessageByString HWND5, WM_SETTEXT, Len(Txt), Txt
                
                'Txt = ""
                'Click the save button
                SendMessage HndB, BM_CLICK, 0, 0
                Exit For
            Else
                HndB = FindWindowEx(hwnd, 0, "Button", "&Open")
                If HndB <> 0 Then
                        GetWindowRect HndB, tRECT
         
                        With tRECT
                            X = .Left + (.Right - .Left) / 2
                            Y = .Top + (.Bottom - .Top) / 2
                        End With
                        
                        SetForegroundWindow hwnd
                        
                        SetCursorPos X, Y
                        'Delay 0.3
                        DoEvents
                        
                        SendMessage HndB, BM_CLICK, 0, 0
                        
                        Exit For
                End If
                'Delay 0.5
            End If
            Debug.Print "K", K
        Next K
         
         
        HWNDCont = FindWindow("#32770", "File Download")
 
        Wend
    xlapp.Visible = True
End Sub

                                  
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:

Select allOpen in new window

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2009-10-29 at 16:43:17ID24856676
Topics

Automation

,

Microsoft Access Database

,

Visual Basic Programming

Participating Experts
2
Points
500
Comments
17

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. automation ftp download
    Is there anyway or any utility to automate downloading a file from a ftp server within a specified time interval, like every second ? the new downloaded file will replace the existing one.
  2. Preventing File Download Dialog box with IE automation
    I'm using an AxSHDocVw.AxWebBrowser to automate signing in, navigating to the correct page, and downloading some information to an Excel file. I've made it to the correct page. Now, how do I prevent the File Download dialog box from poping up and how can I enter the informa...
  3. Automating the IE6 Save As Dialog
    Dear VBA expert. I have successfully opened a browser and executed a javascript function which opens a Save As dialog box with the code below. NOW I would like to be able to automate the Save As dialog box by controlling it's directory path and name... or at the very least ...
  4. How to automated download file, without displaing a dialo…
    I want to download a file programaticaly in a especific URL using Visual C++, i´m use Visual Studio 2008 and i want to construct the program to download a file with no download dialog box, i want to download a file directly, developing my automated program download, with no u...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: thenelsonPosted on 2009-10-30 at 08:08:57ID: 25703713

You do not need API function calls to interface with internet explorer.  Take a look at: Internet Explorer Automation in www.thenelson.name/#WebAccess for an example on how to do it. Using the code in the example, you can  find the name of the objects on a page, using this code
For i = 0 To IE.Document.all.length - 1
    If IE.Document.all(i).getAttribute("type") <> "" Then
       Debug.Print "Index: " & i, "Type: " & IE.Document.all(i).getAttribute("type"), "Name: " & IE.Document.all(i).getAttribute("name")
    End If
Next i

 

by: rsburgePosted on 2009-10-30 at 08:43:33ID: 25704075

Thank you for your response.  I do use this method you've mentioned quite a lot, but in this case, I have to press open on the file download dialog because the website I am accessing forces the file download dialog to open.  

I've use several different methods of downloading files, but a few of them require password authentication thus the need for the API functions in this case.

 

by: thenelsonPosted on 2009-10-30 at 08:53:50ID: 25704177

<a few of them require password authentication thus the need for the API functions in this case>
Try accessing the page with
ftp://usernamehere:passwordhere@serveraddresshere
or
http://usernamehere:passwordhere@serveraddresshere

 

by: thenelsonPosted on 2009-10-30 at 08:56:09ID: 25704213

Your can try the above formats in Internet Explorer URL box to test if it works with your site.

 

by: rsburgePosted on 2009-10-30 at 09:17:31ID: 25704426

I have tried this, but it doesn't work in this case because the download button on the site runs a java script that forces the download dialog to appear.  In other words, there is no server address reference to use in the examples above.

 

by: thenelsonPosted on 2009-10-30 at 10:08:02ID: 25704934

I will study your code. I have been looking for a way to enter user name and password into "unassessable" login in pages. See: http:Q_24838887.html

 

by: rsburgePosted on 2009-10-30 at 10:19:02ID: 25705039

Thank you.

I haven't encountered this problem so I don't have any tried and true way to resolve it.  I will see if I can come up with anything different than what is working for you at the moment.

 

by: egl1044Posted on 2009-10-30 at 11:00:28ID: 25705379

Refer to post ID:25592701 and replace the enum with the following. You can use a Timer w/ FindWindow() create a two second interval to check if the dialog is on the screen.

Public Enum BUTTON_ID
[Button_Open] = &H1119'Open/Run
[Button_OpenFolder] = &H1118
[Button_Cancel] = &H2
End Enum

                                              
1:
2:
3:
4:
5:

Select allOpen in new window

 

by: rsburgePosted on 2009-10-30 at 11:16:20ID: 25705509

Thank you for your response.

Right now I am using SendKeys with a timer, but because the amount of time for the dialog to pop up varies for each file on the site, it is taking 12 minutes to download all of the files (46 in all).  My goal is to find a way to "look" for the file download dialog so I don't have to hard code a 6 second wait after clicking on the link while waiting for the file download to appear.  

How can I create a loop to properly wait for the file download dialog to appear?

 

by: egl1044Posted on 2009-10-30 at 11:37:22ID: 25705695

Yes a simple example would be to Create a class module with an event that will kick off when the dialog is present passing off the handle to the dialog. Below you will find a small example.

Add 1 timer to a form. (Set the interval 1-5 seconds)
Add 1 class module

'//
'// Class1.cls
'//
 
Option Explicit
 
' IE Dialog Class
 
Private Declare Function FindWindowW Lib "user32" (ByVal lpClassName As Long, ByVal lpWindowName As Long) As Long
 
Event OnDialogReady(ByVal Handle As Long)
 
'// must be module level.
Dim hDlg As Long
Dim hDlgPrev As Long
 
Public Sub CheckDialogExists()
 
  '// Find the dialog.'#32770 (Dialog)
  hDlg = FindWindowW(0, StrPtr("Download Complete"))
  '// Make sure the dialog exists
  If hDlg <> 0 Then
  '// You must make sure this isn't the same dialog.
    If hDlg <> hDlgPrev Then
      hDlgPrev = hDlg
      RaiseEvent OnDialogReady(hDlg)
    End If
  End If
  
End Sub
 
 
'//
'// Form1.frm
'//
 
Option Explicit
 
Dim WithEvents DlgHandler As Class1
 
Private Sub DlgHandler_OnDialogReady(ByVal Handle As Long)
'// Do something with the dialog here.
  MsgBox "Dlg = " & Handle, vbSystemModal
End Sub
 
Private Sub Form_Load()
  Set DlgHandler = New Class1
End Sub
 
Private Sub Timer1_Timer()
  DlgHandler.CheckDialogExists
End Sub

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:

Select allOpen in new window

 

by: rsburgePosted on 2009-10-30 at 13:16:29ID: 25706437

I'm sorry for the delayed response.  Thank you for the example.  I will test it as soon as I can and let you know if I have any questions.

 

by: rsburgePosted on 2009-10-30 at 15:31:36ID: 25707202

For whatever reason I can't seem to make this work.  This one must be way over my head.  The code I posted above does work when if I run it manually after I get the file download dialog on the screen.  I just think I have more code than I need and I don't know how to make my existing code wait for the file download dialog box to appear.

 

by: egl1044Posted on 2009-10-30 at 16:29:31ID: 25707442

Did you change this line:
hDlg = FindWindowW(0, StrPtr("Download Complete"))
TO:
hDlg = FindWindowW(0, StrPtr("File Download"))

We may be talking about different dialogs here so can you post a screen shot. I don't have such dialog for IE8 on Windows 7. I only get the security dialog(save,run,cancel) and then the (Download Complete) dialog.

 

by: rsburgePosted on 2009-10-30 at 16:42:01ID: 25707498

Hi - It is the file download dialog; screen shot attached.  I think my problem is that I am not putting all of the code in the right places.

I added a class module and added the code you provided and changed the piece you mention above.  Does all of the code you provided go into the class module and then what do I put into my existing code to wait for and then do the button click on the file download dialog?  I can hardcode sleep 5500 to wait for the dialog, but I want to stop doing that because sometimes it takes 5 seconds, sometimes less and sometimes more.



 

by: egl1044Posted on 2009-10-30 at 17:39:32ID: 25707774

Okay. Start a new standard project.

Add 1 class module named: FileDownloadDlg
Add 1 Timer control

When you run the project it will look for the dialog when it finds the dialog it will trigger the event we created in the class module. I added a (Click) method to help you understand this better. Hope that helps. This will always look for the dialog so if you need to stop it then just disable the timer but I think you will understand the concept now.

I have no idea what EE is doing to the site right now but they are changing stuff while I am posting this it seems they are changing how things are posted in the expert mode. Hopefully what I have added as the code snippet comes out correctly. :)

egl

'//
'// Add this to FileDownloadDlg
'//
 
Option Explicit
 
' IE8 dialog automation example.
 
Private Const DIALOG_TITLE As String = "File Download"
 
Private Const BM_CLICK = &HF5&
Public Enum DIALOG_ID
[Button_Open] = &H114A
[Button_Save] = &H114B
[Button_Cancel] = &H2
End Enum
 
Private Declare Function FindWindowW Lib "user32" (ByVal lpClassName As Long, ByVal lpWindowName As Long) As Long
Private Declare Function SendDlgItemMessageW Lib "user32" (ByVal hDlg As Long, ByVal nIDDlgItem As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
 
Event OnDialogReady(ByVal Handle As Long)
 
Dim hDlg As Long
Dim hDlgPrev As Long
 
Public Sub CheckForDialog()
  '// Find the dialog.
  hDlg = FindWindowW(0, StrPtr(DIALOG_TITLE))
  '// Make sure the dialog exists
  If hDlg <> 0 Then
  '// You must make sure this isn't the same dialog.
    If hDlg <> hDlgPrev Then
      hDlgPrev = hDlg
      RaiseEvent OnDialogReady(hDlg)
    End If
  End If
End Sub
 
Public Sub Click(ByVal hDlg As Long, BtnId As DIALOG_ID)
'// Clicks one of the buttons on the dialog.
SetForegroundWindow hDlg
Sleep 1000
SendDlgItemMessageW hDlg, BtnId, BM_CLICK, 0, 0
End Sub
 
 
 
 
 
 
 
 
 
 
'//
'// Add this to Form1
'//
 
 
Option Explicit
 
Dim WithEvents Dialog As FileDownloadDlg
 
Private Sub Dialog_OnDialogReady(ByVal Handle As Long)
  Dialog.Click Handle, Button_Open
End Sub
 
Private Sub Form_Load()
  Set Dialog = New FileDownloadDlg
  Timer1.Enabled = True
  Timer1.Interval = 3000
End Sub
 
Private Sub Timer1_Timer()
  Dialog.CheckForDialog
End Sub
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:

Select allOpen in new window

 

by: rsburgePosted on 2009-10-30 at 20:22:19ID: 25708210

Thank you so much for your help.  I think this is working for me now.  I am testing it further to be sure.  I will respond back as soon as possible.

 

by: rsburgePosted on 2009-11-01 at 09:41:39ID: 31647810

Thank you for all of your help!

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...