MikeGribble
asked on
Extact message from a message box
I need to read the actual message text from a standard Message Box using VBA (Access 2000)
The message box may be thrown up by my app or some other app but either way I CAN find the HWnd of the message box I am interested in but using sendmessage with WM_GetText or GetWindowText both bring back the Title Bar Text.
I Have tried virtually every suggestion on the web but still get the title!
How can I bring back the actual message text?
The message box may be thrown up by my app or some other app but either way I CAN find the HWnd of the message box I am interested in but using sendmessage with WM_GetText or GetWindowText both bring back the Title Bar Text.
I Have tried virtually every suggestion on the web but still get the title!
How can I bring back the actual message text?
You only obtain the title because you have to go deeper into the static area of the dialog.
Dim hDlg As Long
hDlg = FindWindowW(StrPtr("#32770 "), 0)
Debug.Print GetDialogText(hDlg, ID_STATIC)
Dim hDlg As Long
hDlg = FindWindowW(StrPtr("#32770
Debug.Print GetDialogText(hDlg, ID_STATIC)
Option Explicit
Private Const WM_GETTEXT As Long = &HD&
Private Const SMTO_NORMAL As Long = &H0&
Private Const ID_STATIC As Long = &HFFFF&
Private Declare Function FindWindowW Lib "user32" ( _
ByVal lpClassName As Long, _
ByVal lpWindowName As Long) As Long
Private Declare Function GetDlgItem Lib "user32" ( _
ByVal hDlg As Long, _
ByVal nIDDlgItem As Long) As Long
Private Declare Function SendMessageTimeoutW Lib "user32.dll" ( _
ByVal hWnd As Long, _
ByVal Msg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long, _
ByVal fuFlags As Long, _
ByVal uTimeout As Long, _
ByRef lpdwResult As Long) As Long
Public Function SendDlgItemMessageTimeout( _
ByVal hWnd As Long, _
ByVal Msg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long
Dim Success As Long
Dim lpResult As Long
Success = SendMessageTimeoutW(hWnd, Msg, wParam, lParam, _
SMTO_NORMAL, 1000, lpResult)
SendDlgItemMessageTimeout = lpResult
End Function
Public Function GetDialogText( _
ByVal hDlg As Long, _
ByVal dwId As Long) As String
' Returns text associated with the control.
Dim Buffer(1024 * 2) As Byte
Dim cbSize As Long
Dim hWndId As Long
hWndId = GetDlgItem(hDlg, dwId)
cbSize = SendDlgItemMessageTimeout(hWndId, WM_GETTEXT, 1024, VarPtr(Buffer(0)))
If cbSize <> 0 Then
GetDialogText = Left$(Buffer, cbSize)
Else
GetDialogText = vbNullString
End If
Erase Buffer
End Function
<GetWindowText both bring back the Title Bar Text.>
Then the handle you are using for the GetWindowText is the handle to the title and not the actual message text.
Looking at a standard Message Dialog in Access 2003 - I see 3 parts to it. The Dialog has a handle - its text is the title. Then there are 2 children of this one - the first is the OK button and the 2nd is the text.
Try getting a handle on the child of handle that shows the title text. What text does this handle have?
Do you have some code?
Then the handle you are using for the GetWindowText is the handle to the title and not the actual message text.
Looking at a standard Message Dialog in Access 2003 - I see 3 parts to it. The Dialog has a handle - its text is the title. Then there are 2 children of this one - the first is the OK button and the 2nd is the text.
Try getting a handle on the child of handle that shows the title text. What text does this handle have?
Do you have some code?
ASKER
I've tried your solution but get a zero length string
An Excel save changes message box is up when running your solution
hDlg = FindWindowW(StrPtr("#32770 "), 0) returns 1706052
and I have no reason to doubt this is correct because when I run it against a message box generated by my own app I know it returns the handle to that message box
I has hoped that GetDlgItem was what would help me but....
Debug.Print GetDialogText(hDlg, ID_STATIC) Returns a zero length string
Stepping through the GetDialogText function shows hDlg as 1706052 and dwId as 65535
hWndId = GetDlgItem(hDlg, dwId) returns hWndId as 0
cbSize = SendDlgItemMessageTimeout( hWndId, WM_GETTEXT, 1024, VarPtr(Buffer(0))) thus naturally returns 0
An Excel save changes message box is up when running your solution
hDlg = FindWindowW(StrPtr("#32770
and I have no reason to doubt this is correct because when I run it against a message box generated by my own app I know it returns the handle to that message box
I has hoped that GetDlgItem was what would help me but....
Debug.Print GetDialogText(hDlg, ID_STATIC) Returns a zero length string
Stepping through the GetDialogText function shows hDlg as 1706052 and dwId as 65535
hWndId = GetDlgItem(hDlg, dwId) returns hWndId as 0
cbSize = SendDlgItemMessageTimeout(
The example will find the first dialog with class name #32770 which means if there is more than one window available with the classname the handle could be one of the other windows. This is common as #32770 is the static classname for all dialogs in windows.
You can verify it works but to do that you will need to narrow the search to ensure you return the correct handle to the dialog your looking for you can do that by enumerating the windows or specify the title of the message box.
You can verify it works but to do that you will need to narrow the search to ensure you return the correct handle to the dialog your looking for you can do that by enumerating the windows or specify the title of the message box.
hDlg = FindWindowW(StrPtr("#32770"), StrPtr("messagebox title/caption"))
ASKER
Both hDlg = FindWindowW(StrPtr("#32770 "), 0) and hDlg = FindWindowW(StrPtr("#32770 "), StrPtr("Microsoft Excel"))
return the same handle as expected (I do not have a problem with identifying which dialog belongs to the handle
I had expected GetDialogText to return "Do you want save the changes to ........................
but I am getting a zero length string - that is the problem
return the same handle as expected (I do not have a problem with identifying which dialog belongs to the handle
I had expected GetDialogText to return "Do you want save the changes to ........................
but I am getting a zero length string - that is the problem
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Perfect - works fine and gives me exactly what I need - Thanks & well done
Makes the subscription worth it
Makes the subscription worth it
if this is what you are after, you can get a list of this errors and description
see this link
http://support.microsoft.com/kb/105666