VB and VBA Users Source Code: Return a dialog handle/class name from a caption
[
Home
|
Contents
|
Search
|
Reply
| Previous | Next ]
VB/VBA Source Code
Return a dialog handle/class name from a caption
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Thursday, February 22, 2001
Hits:
950
Category:
Windows API
Article:
Most form related API calls require a window handle and unfortunately VBA doesn't provide these. The following routines will return both a forms window handle and it's class name from the forms caption. Private Declare Function FindWindowA Lib "user32" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long 'Purpose : Returns the Windows Handle of a Dialog based on its caption. 'Inputs : sDialogCaption The dialog caption. ' [sClassName] The class name of the dialog. If unknown, do not specify ' this parameter. 'Outputs : The Dialogs Window Handle 'Author : Andrew Baker 'Date : 30/05/2000 'Notes : 1/ Find windows scans down the Z Order of the dialogs currently displayed, ' 2/ To Call in a VBA form use: lHwnd = DialogGetHwnd(Me.Caption) ' 3/ Class names: ' "bosa_sdm_XL" Excel 5 Form class name ' "ThunderDFrame" Excel 97/2000 Form class name ' 4/ Use Spy ++ (comes with Visual Studio), OR the function DialogGetClassName to ' return the class names of forms. 'Revisions : Function DialogGetHwnd(ByVal sDialogCaption As String, Optional sClassName As String = vbNullString) As Long On Error Resume Next DialogGetHwnd = FindWindowA(sClassName, sDialogCaption) On Error GoTo 0 End Function 'Purpose : Returns the class name of a object given the handle or caption 'Inputs : [sCaption] = The objects caption ' [lHwnd] = The objects handle 'Outputs : 'Author : Andrew Baker 'Date : 12/08/2000 12:33 'Notes : Pass in either sCaption OR lHwnd, to get a class name 'Revisions : Function DialogGetClassName(Optional sCaption As String, Optional lHwnd As Long) As String Const clMaxLen As Long = 256 Dim lRetVal As Long, sResult As String * clMaxLen If Len(sCaption) Then 'Get Dialog Handle lHwnd = DialogGetHwnd(sCaption) End If If lHwnd Then 'Get Class Name lRetVal = GetClassName(lHwnd, sResult, clMaxLen) DialogGetClassName = Left$(sResult, lRetVal) End If End Function
Terms and Conditions
Support this site
Download a trial version of the Excel Workbook Rebuilder