Solved

Extact message from a message box

Posted on 2010-09-20
8
594 Views
Last Modified: 2012-05-10
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?
0
Comment
Question by:MikeGribble
8 Comments
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 33716041
most of this app generated messages are error description,
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
0
 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
ID: 33716167
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)

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

Open in new window

0
 
LVL 19

Expert Comment

by:darbid73
ID: 33716235
<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?


0
 

Author Comment

by:MikeGribble
ID: 33719338
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

0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
ID: 33719417
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.

hDlg = FindWindowW(StrPtr("#32770"), StrPtr("messagebox title/caption"))

Open in new window

0
 

Author Comment

by:MikeGribble
ID: 33719510
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
 
0
 
LVL 29

Accepted Solution

by:
nffvrxqgrcfqvvc earned 500 total points
ID: 33719846
The dialog you refer to is a custom dialog by excel not the traditional messagebox you would get if you did MsgBox(etc..) which the previous code was expecting.
 MsgBox GetDialogTextEx

Option Explicit



Private Const BUF_SIZE As Long = 1024

Private Const WM_GETTEXT As Long = &HD&

Private Const SMTO_NORMAL As Long = &H0&



Private Declare Function FindWindowExW Lib "user32" ( _

  ByVal hwndParent As Long, _

  ByVal hwndchildAfter As Long, _

  ByVal lpszClass As Long, _

  ByVal lpszWindow 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 GetDialogTextEx() As String

  Dim Buffer(BUF_SIZE * 2)  As Byte

  Dim hWindow               As Long

  Dim dwResult              As Long

  Dim cbSize                As Long

  

  hWindow = FindWindowExW(0, 0, StrPtr("#32770"), 0)

  hWindow = FindWindowExW(hWindow, 0, StrPtr("MSOUNISTAT"), 0)

  

  If hWindow = 0 Then

    Debug.Print "No Dialog exists"

    Exit Function

  End If

  

  Call SendMessageTimeoutW(hWindow, WM_GETTEXT, BUF_SIZE, _

    VarPtr(Buffer(0)), SMTO_NORMAL, 1000, dwResult)

    

  If dwResult <> 0 Then

      GetDialogTextEx = Left$(Buffer, dwResult - 1)

    Else

      GetDialogTextEx = vbNullString

  End If

  

End Function

Open in new window

0
 

Author Comment

by:MikeGribble
ID: 33719972
Perfect - works fine and gives me exactly what I need - Thanks & well done

Makes the subscription worth it
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now