Accessing buttons on the Internet Explorer 11 View downloads form

81 Views Asked by At

I have a piece of code in vb.net to open a web link for an application and to download a file from the page. The web browser loads the page and the program auto clicks the link on the page to download the file which then makes the "View Downloads - Internet Explorer" window in IE 11 to pop up as shown below:

Internet Explorer 11- VIew Downloads window

I managed to activate the window but not able to click the Save button to automatically download the file without user intervention. The code that I tried to activate and click the button is as below

Private Declare Auto Function FindWindow Lib "user32" (ByVal ClassName As String, ByVal WindowTitle As String) As IntPtr
Private Declare Auto Function FindWindowEx Lib "user32" (ByVal parentHandle As IntPtr, ByVal childAfter As IntPtr, ByVal ClassName As String, ByVal WindowTitle As String) As IntPtr
Private Declare Auto Function SendMessage Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wParam As Integer, ByVal lParam As IntPtr) As IntPtr


Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
    Dim ParentWndHandle As IntPtr
    ParentWndHandle = FindWindow("#32770", "View Downloads - Internet Explorer")
    If ParentWndHandle <> IntPtr.Zero Then
        Dim ChildWndHandle As IntPtr
        ChildWndHandle = FindWindowEx(ParentWndHandle, vbNullString, "DirectUIHWND", vbNullString)
        Dim btn1 As IntPtr = FindWindowEx(ChildWndHandle, IntPtr.Zero, "Button", "&Save")
        SendMessage(ChildWndHandle, WM_ACTIVATE, WA_ACTIVE, 0)
        SendMessage(ChildWndHandle, WM_COMMAND, VK_RETURN, IntPtr.Zero)
    End If
End Sub
1

There are 1 best solutions below

2
Yu Zhou On

I think your issue might be the same as this thread: You need to use SetActiveWindow before SendMessage.

Or can you extract the file url? If so, you can refer to this doc to use the simple DownloadFile method to download files:

My.Computer.Network.DownloadFile(
    "http://www.example.com/downloads/abc.txt",
    "C:\Documents and Settings\All Users\Documents\abc.txt")