IE Diologue Box Help

vmi1839

New Member
Joined
Apr 1, 2003
Messages
39
I've created a macro that will open Internet Explorer (setting IE as an object), wait for a web page to load and then press a button on that page to download a file. The problem that I am running into is that when the button for file download is pressed a diologue box pops up asking what I would like to to with the file (i.e. open, save, cancel, etc). Given that this is a .csv file (MS Excel), I am unable to "uncheck" the box that would prevent this diologue box from poping up.

The real problem occurs when IE takes a while to load the dialogue box causing my macro to go screwy . I need a way to tell the macro to keep waiting until the diologue box appears before continuing. Either that or find a way to get rid of the diologue box all together.

I know this is probably more confusing than helpful but I'm at a loss as to how else to explain the situation. Any help at all would be appreciated.
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Hi vmi1839

Just spent some time writing this that you will need to integrate into your code, if you choose to use it.

This uses the Win32 API to find the File Download dialog, once it finds that, it enumerates the child windows of that dialog to find the Save button and sends a click message to it. Once that button has been clicked, we see the Save As dialog, so we find that. Then again it enumerates the child windows of the dialog to find the Save As button again, and sends a click message to that.

PROBLEMS:

I haven't done a search for the file first, so if the file already exisits this will be a problem.

I was having some problems in waiting for the window to paint fully, so being tired/lazy/ready for bed, I just threw some sleeps in, you may be able to fix this.

OK, the code: in your userform, sheet, whatever code sheet add this code:

Code:
Option Explicit
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Integer, ByVal lParam As Any) As Long
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Private Const BM_CLICK As Long = &HF5

Private Sub LookForWindow()
Dim hwnd As Long, lngRet As Long

'look for the File Download window
Do While hwnd = 0
    hwnd = FindWindow("#32770", "File Download")
Loop

'having a few problems with paint delays
Sleep 500

'now look for the button with &Save as a title
EnumChildWindows hwnd, AddressOf EnumChildProc, ByVal 0&

'reset hwnd
hwnd = 0

Sleep 500

'send a click message to the button
lngRet = SendMessage(hwndBtn, BM_CLICK, 0, vbNullString)

hwndBtn = 0

'wait for the Save As window
Do While hwnd = 0
    hwnd = FindWindow("#32770", "Save As")
Loop

Sleep 500

'now look for the button with &Save as a title
EnumChildWindows hwnd, AddressOf EnumChildProc, ByVal 0&

'send a click message to the button
lngRet = SendMessage(hwndBtn, BM_CLICK, 0, vbNullString)

End Sub

In a module, and that is important or the AddressOf Operator won't work, add this code:

Code:
Declare Function EnumChildWindows Lib "user32" (ByVal hWndParent As Long, ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
Global hwndBtn As Long

Public Function EnumChildProc(ByVal hwnd As Long, ByVal lParam As Long) As Long
    Dim strSave As String
    'We need to get the length of the text in the window
    strSave = Space$(GetWindowTextLength(hwnd) + 1)
    'get the window text
    GetWindowText hwnd, strSave, Len(strSave)
    'ditch the last character
    strSave = Left$(strSave, Len(strSave) - 1)
    'as both our buttons have text of Save, we can get out here
    If Left(strSave, 5) = "&Save" Then
        hwndBtn = hwnd
        Exit Function
    End If
    EnumChildProc = 1
End Function

I hope this gets you on your way, if you can't fix the issues I have listd, maybe someone else here can, I am off to bed and then off for a few days as it's the May day holiday \o/

HTH
 
Upvote 0

Forum statistics

Threads
1,214,834
Messages
6,121,871
Members
449,055
Latest member
excelhelp12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top