Cannot download file from IE11 dialog box using vba

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading...
4.26K views
0

Hi,
I have created a code which will login in a website and when it click on export to download a file, IE displays a dialog box stating open,save and save as button inspite of several attempts my code doesn’t clicks on save button,following is my code:

Sub Save_Over_Existing_Click_Yes()

Dim hwnd As Long
Dim timeout As Date

Debug.Print “Save_Over_Existing_Click_Yes”

‘Find the Download complete window, waiting a maximum of 30 seconds for it to appear. Timeout value is dependent on the
‘size of the download, so make it longer for bigger files

timeout = Now + TimeValue(“00:00:40”)
Do
hwnd = FindWindow(“#32770”, “Internet Explorer”)
MsgBox hwnd
DoEvents
Sleep 200
Loop Until hwnd Or Now > timeout
‘ Debug.Print ” Save As window “; Hex(hwnd)

If hwnd Then
‘Find the child Close button
hwnd = FindWindowEx(hwnd, ByVal 0&, “Button”, vbNullString)

‘hwnd = FindWindowEx(hwnd, 0, “Button”, “&Save”)
‘ Debug.Print ” save button “; Hex(hwnd)
End If
MsgBox hwnd
If hwnd Then

‘Click the Close button
MsgBox 1
SetForegroundWindow (hwnd)
Sleep 6000 ‘this sleep is required and 600 miiliseconds seems to be the minimum that works
Const BM_CLICK As Long = &HF5&
SendMessage hwnd, BM_CLICK, 0, 0
End If
End Sub

Thanks,
Ramandeep Singh

edited answer
0

Personally I do not think using FindWindow is very reliable. Try simply simulating the “Save” button like so:

commented on answer
Avatar

Thanks @anaystcave for your reply, but this application.sendkeys doesnot work when system is locked. how can we handle this??

Tom (AnalystCave)

Correct won’t work then. Does the page generate a download url? If so my Web Scraping kit contains a download file procedure based on an url no IE required. Otherwise i would revert to Selenium but downloading files will still be a challenge. Can u share the webpage?

Avatar

I’m able to download the file now but a new problem has arrived i.e. if I open multiple tabs for example there are 4 tabs currently opened and tab 2 contains the dialog box for saving the file but I can’t move between the tabs as there is no separate handler for these tabs. I there any way we can move to 2nd tab??

Simply the best place to learn Excel VBA