Problem : Set Focus to Modeless VBA Userform

Problem : Set Focus to Modeless VBA Userform

Hi,

Could you please tell me how to set the focus to a modeless VBA userform?  Individual controls have a function named SetFocus(), but userforms do not have this function.


Solution : Set Focus to Modeless VBA Userform

You are going to need some extra code to do this. Replace “UserForm1” in the code below with the name of your user form.

Private Declare Function FindWindow Lib “user32” Alias “FindWindowA” ( _
ByVal lpClassName As String, _
ByVal lpWindowName As String _
) As Long

Private Declare Function SetForegroundWindow Lib “user32” ( _
ByVal hwnd As Long _
) As Long

Public Sub ActivateUserForm()

SetForegroundWindow DialogHWnd(UserForm1)

End Sub

Public Function DialogHWnd( _
ByRef WindowObject As Object _
) As Long

‘ Return the hWnd value for the window.

If TypeName(WindowObject) = “DialogSheet” Then
Select Case (CDbl(Application.Version))
Case 7 ‘ Excel 95
DialogHWnd = GetWindowFromTitle(WindowObject.DialogFrame.Caption, “bosa_sdm_XL”)
Case 8 ‘ Excel 97
DialogHWnd = GetWindowFromTitle(WindowObject.DialogFrame.Caption, “bosa_sdm_XL8”)
Case 9 ‘ Excel 2000
DialogHWnd = GetWindowFromTitle(WindowObject.DialogFrame.Caption, “bosa_sdm_XL9”)
Case Else
Exit Function
End Select
Else
Select Case (CDbl(Application.Version))
Case 8 ‘ Excel 97
DialogHWnd = GetWindowFromTitle(WindowObject.Caption, “ThunderXFrame”)
Case Is >= 9 ‘ Excel 2000 or later
DialogHWnd = GetWindowFromTitle(WindowObject.Caption, “ThunderDFrame”)
Case Else
Exit Function
End Select
End If

End Function

Public Function GetWindowFromTitle( _
ByVal WindowTitle As String, _
Optional ByVal ClassName As String _
) As Long

‘ Find the window handle of the window with the class and name provided.

Dim hwnd As Long

If Len(ClassName) = 0 Then
hwnd = FindWindow(vbNullString, WindowTitle)
Else
hwnd = FindWindow(ClassName, WindowTitle)
End If

GetWindowFromTitle = hwnd

End Function