Solved

Set Focus to Modeless VBA Userform

Posted on 2007-11-24
3
4,260 Views
Last Modified: 2012-05-05
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.
0
Comment
Question by:deatc
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 81

Accepted Solution

by:
zorvek (Kevin Jones) earned 500 total points
ID: 20343677
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

Kevin
0
 
LVL 81

Expert Comment

by:zorvek (Kevin Jones)
ID: 20343680
The above assumes you are working in Excel. Is that true?

Kevin
0
 

Author Comment

by:deatc
ID: 20400608
Thanks, Kevin!
0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Office Picture Manager was included in Office 2003, 2007, and 2010, but not in Office 2013. Users had hopes that it would be in Office 2016/Office 365, but it is not. Fortunately, the same zero-cost technique that works to install it with …
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
Suggested Courses
Course of the Month5 days, 7 hours left to enroll

627 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