I must send the click evet to the OK button from an other application.
Please help me.
Printable View
I must send the click evet to the OK button from an other application.
Please help me.
try to use API f:SendMessage
My API Literay are bad. Please could you halp me :confused:
VB Code:
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 Long, lParam As Any) As Long Private Const WM_NCLBUTTONDOWN = &HA1 Private Sub cmdCloseApp_Click() Dim CloseIt As Long CloseIt = FindWindow(vbNullString, "Microsoft Outlook") SendMessage CloseIt, WM_NCLBUTTONDOWN, CLng(0), CLng(0) End Sub
Thank you very mouch!
Send the BM_CLICK message to the window.
How can i tell him which button? :(
It send something but not to the OK button.
VB Code:
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 Long, lParam As Any) As Long Private Const BM_CLICK = &HF5 Private Sub cmdCloseApp_Click() Dim CloseIt As Long CloseIt = FindWindow(vbNullString, "Microsoft Outlook") SendMessage CloseIt, BM_CLICK, CLng(0), CLng(0) End Sub
Send BM_CLICK to the actual button (not the parent window).
To get the handle of the button, use the FindWindowEx function. e.g.
hwndButton = FindWindowEx(hwndParent, 0, "Button", "Button Text")
I've had to do this before. Once you find the windows using find window, you'll have to use use the EnumChildWindows API.
you'll hva
Here's some code to get you started.
Public Declare Function EnumChildWindows Lib "user32" (ByVal hWndParent As Long, ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Public Declare Function SendMessageByStr& Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String)
Private Function GetChildControls(apphwnd As Long)
EnumChildWindows apphwnd, AddressOf EnumChildProc, ByVal 0&
End Function
Public Function EnumChildProc(ByVal hwnd As Long, ByVal lParam As Long) As Long
Dim sSave As String
Dim sClassName As String
Dim sWindowText As String
Dim lControlId As Long
'// Get window class name
ReDim Preserve Wlist(UBound(Wlist) + 1)
sClassName = String(255, Chr(0))
GetClassName hwnd, sClassName, 255
sClassName = Left(sClassName, InStr(1, sClassName, Chr(0), vbTextCompare) - 1)
sWindowText = String(255, Chr(0))
SendMessageByStr hwnd, WM_GETTEXT, 255, sWindowText
'if the text is &OK or whatever you're looking for, use sendmessage to click the button.
sWindowText = Left(sWindowText, InStr(1, sWindowText, Chr(0), vbTextCompare) - 1)
lControlId = GetWindowLong(hwnd, GWL_ID)
'// continue enumeration
EnumChildProc = 1
End Function
Another way to go about this is to use FindWindowEx again; but this time, start at the previous window e.g.
hChild = FindWindowEx(hParent, hChild, "Button", vbNullstring)
This will enumerate all buttons.
But again, you'd only need to do this if the button does not have a caption. If it does, then you should get it on the first try.
I have only the capion of the bottom and only the caption from the window.
Please help
try this :
CloseIt = FindWindow(vbNullString, "Microsoft Outlook")
hwndButton = FindWindowEx(CloseIt, 0, "Button", "OK")
SendMessage hwndButton, BM_CLICK, CLng(0), CLng(0)
and tell me if it works becouse i forgot how it works i mean this functions FindWindowEx, i want to refresh my memory
:):)It Works!!!!!!!!!!!:):)
Thank you all for the great help.:cool:
hire is my current code
VB 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 Long, lParam As Any) 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 Const BM_CLICK = &HF5 Private Sub cmdCloseApp_Click() Dim CloseIt As Long Dim hwndbutton As Long CloseIt = FindWindow(vbNullString, "Microsoft Outlook") hwndbutton = FindWindowEx(CloseIt, 0, vbNullString, "OK") SendMessage hwndbutton, BM_CLICK, CLng(0), CLng(0) End Sub
You're welcome :)
It's usually a good idea to specify the classname as well e.g.
It just helps narrow your search down a little more.Code:hwndbutton = FindWindowEx(CloseIt, 0, "Button", "OK")
What's the parameter if it's a dropdown menu or combo box?
-Roy