Getting Error in this code.

Dinesh Chauhan

New Member
Joined
Dec 29, 2015
Messages
5
Platform
  1. Windows
VBA Code:
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 GetWindowText Lib "user32" Alias "GetWindowTextA" _


(ByVal hWND As Long, ByVal lpString As String, ByVal cch As Long) As Long





Private Declare Function GetWindowTextLength Lib "user32" Alias _


"GetWindowTextLengthA" (ByVal hWND As Long) As Long





Private Declare Sub SetWindowPos Lib "user32" (ByVal hWND As Long, ByVal _


hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As _


Long, ByVal cy As Long, ByVal wFlags As Long)





Private Declare Function SetCursorPos Lib "user32" _


(ByVal X As Integer, ByVal Y As Integer) As Long





Private Declare Function GetWindowRect Lib "user32" _


(ByVal hWND As Long, lpRect As RECT) As Long





Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)





Private Declare Sub mouse_event Lib "user32.dll" (ByVal dwFlags As Long, _


ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)





'~~> Constants for pressing left button of the mouse


Private Const MOUSEEVENTF_LEFTDOWN As Long = &H2


'~~> Constants for Releasing left button of the mouse


Private Const MOUSEEVENTF_LEFTUP As Long = &H4





Private Type RECT


    Left As Long


    Top As Long


    Right As Long


    Bottom As Long


End Type





Const HWND_TOPMOST = -1


Const HWND_NOTOPMOST = -2


Const SWP_NOSIZE = &H1


Const SWP_NOMOVE = &H2


Const SWP_NOACTIVATE = &H10


Const SWP_SHOWWINDOW = &H40





Dim Ret As Long, ChildRet As Long, OpenRet As Long


Dim strBuff As String, ButCap As String


Dim pos As RECT





Sub MsgPopUpClick()


    '~~> Get the handle of the "File Download" Window


'Call MsgBoxTest


  


    Ret = FindWindow(vbNullString, "Message from webpage")


  


    If Ret <> 0 Then


        'MsgBox "Main Window Found"





        '~~> Get the handle of the Button's "Window"


        ChildRet = FindWindowEx(Ret, ByVal 0&, "Button", vbNullString)





        '~~> Check if we found it or not


        If ChildRet <> 0 Then


            'MsgBox "Child Window Found"





            '~~> Get the caption of the child window


            strBuff = String(GetWindowTextLength(ChildRet) + 1, Chr$(0))


            GetWindowText ChildRet, strBuff, Len(strBuff)


            ButCap = strBuff


          


            '~~> Loop through all child windows


            Do While ChildRet <> 0


                '~~> Check if the caption has the word "Open"


                '~~> For "Save" or "Cancel", replace "Open" with


                '~~> "Save" or "Cancel"


                If InStr(1, Trim(ButCap), "OK") > 0 Then


                    '~~> If this is the button we are looking for then exit


                    'SendKeys ("{enter}")


                    OpenRet = ChildRet


                    Exit Do


                End If





                '~~> Get the handle of the next child window


                ChildRet = FindWindowEx(Ret, ChildRet, "ok", vbNullString)


                '~~> Get the caption of the child window


                strBuff = String(GetWindowTextLength(ChildRet) + 1, Chr$(0))


                GetWindowText ChildRet, strBuff, Len(strBuff)


               ButCap = strBuff


            Loop





            '~~> Check if we found it or not


            If OpenRet <> 0 Then


                'MsgBox "The Handle of Open Button is : " & OpenRet





                '~~> Retrieve the dimensions of the bounding rectangle of the


                '~~> specified window. The dimensions are given in screen


                '~~> coordinates that are relative to the upper-left corner of the screen.


                GetWindowRect OpenRet, pos





                '~~> Move the cursor to the specified screen coordinates.


                SetCursorPos (pos.Left - 10), (pos.Top - 10)


                '~~> Suspends the execution of the current thread for a specified interval.


                '~~> This give ample amount time for the API to position the cursor


                Sleep 100


                SetCursorPos pos.Left, pos.Top


                Sleep 100


                SetCursorPos (pos.Left + pos.Right) / 2, (pos.Top + pos.Bottom) / 2





                '~~> Set the size, position, and Z order of "File Download" Window


                SetWindowPos Ret, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOACTIVATE Or SWP_SHOWWINDOW Or SWP_NOMOVE Or SWP_NOSIZE


                Sleep 100





                '~~> Simulate mouse motion and click the button


                '~~> Simulate LEFT CLICK


                mouse_event MOUSEEVENTF_LEFTDOWN, (pos.Left + pos.Right) / 2, (pos.Top + pos.Bottom) / 2, 0, 0


                Sleep 700


                '~~> Simulate Release of LEFT CLICK


               mouse_event MOUSEEVENTF_LEFTUP, (pos.Left + pos.Right) / 2, (pos.Top + pos.Bottom) / 2, 0, 0


            Else


                'MsgBox "The Handle of Open Button was not found"


            End If


        Else


            'MsgBox "Child Window Not Found"


        End If


    Else


        'MsgBox "Window Not Found"


    End If


End Sub
 
Last edited by a moderator:

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Help us to help you - what is the error and where does it occur?
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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