Reading the ShowModal Property of UserForm at run time ?

Jaafar Tribak

Well-known Member
Joined
Dec 5, 2002
Messages
9,779
Office Version
  1. 2016
Platform
  1. Windows
Is there a way to read the ShowModal Property at runtime ?

Something like this :
Code:
Private Sub UserForm_Activate()
    MsgBox Me.ShowModal
End Sub
Which obviously generates a compile error

Regards.
 
Last edited:

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
I can't seem to find a native way to determine if a userform is Modal or Modeless at run time

I had to resort to the Windows API to find an cheesy workaround. The following custom function IsFormModal seems to work

Code:
Option Explicit

#If VBA7 Then
    Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr
    Private Declare PtrSafe Function SetFocus Lib "user32" (ByVal hwnd As LongPtr) As LongPtr
#Else
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Private Declare Function SetFocus Lib "user32" (ByVal hwnd As Long) As Long
#End If

Private Sub UserForm_Activate()
    If IsFormModal(Me) Then
        MsgBox Me.Name & " is modal"
    Else
        MsgBox Me.Name & " is modeless"
    End If
End Sub

Private Function IsFormModal(Frm As Object) As Boolean
    #If VBA7 Then
        Dim hwnd As LongPtr
    #Else
        Dim hwnd As Long
    #End If

    IsFormModal = Not CBool(SetFocus(Application.hwnd))
    hwnd = FindWindow("ThunderDFrame", Frm.Caption)
    If hwnd Then
        Call SetFocus(hwnd)
    End If
End Function
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,435
Members
452,326
Latest member
johnshaji

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top