Visual Basic > Forms
Return a dialog handle-class name from a caption
Return a dialog handle-class name from a caption 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 '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 : 'Notes : Pass in either sCaption OR lHwnd, to get a class name 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
Visual Basic Codes
ActiveX
Miscellaneous
Applications
Code Snippets
Common Dialogs
Special Effects
Database Stuff
Date Time
Files Drives
Forms
Graphics Games
Internet Stuff
Multimedia
Other
Strings
Windows