Closing a Userform

cs1ctp

Board Regular
Joined
Oct 7, 2005
Messages
122
Hi,

Is there anyway to disable the close button in the top right hand corner of a UserForm? I am preparing a password dialog, but at the moment, the user can simply bypass the password entry by simply closing the dialog box. Security becomes void!!

Thanks

cHrIs
 
Hi Again,

That will stop the 'x' from doing anything you can add a msgbox to that procedure informing the user or not.

Don't know if its of any use to you but following on from what Andy said you could add this code as well which will show your message box and then close it after 3 seconds. Or change the 3 to a 4 for 4 seconds and so on.

Code:
Dim WScript As Object 
Set WScript = CreateObject("WScript.Shell") 
WScript.Popup "Write your message here", 3, "title of msgbox here" 
Set WScript = Nothing

Hope this helps
 
Upvote 0

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Like it Charllie, thats very user friendly, or is it. The user clicks on the cross, expects the box to disappear instead up pops a warning, they read the warning and just as they go to press the x on that window it disappears and the original window reappears, broken mice everywhere!! Its GENIUS!!! lol
 
Upvote 0
The only thing that would be better is if the msgbox came up long enough to see it, but not acutally read it before it disappears. :devilish:
 
Upvote 0
And maybe flash a few times with obvious different messages also showing not long enough to read, we could have tech support replacing mice for ages.
 
Upvote 0
Hi Andy,

Sorry mate, my apologises. Had a bad day and just spent ages on net looking for car insurance.

I took it the wrong way and i apologise to you.
 
Upvote 0
cs1ctp said:
Hi,

Is there anyway to disable the close button in the top right hand corner of a UserForm? I am preparing a password dialog, but at the moment, the user can simply bypass the password entry by simply closing the dialog box. Security becomes void!!

Thanks

cHrIs

How about this, no 'x' to click:

Code:
'Highlight the userform in the editor and select code

'Before any subs

Private Const GWL_STYLE = (-16)
Private Const WS_SYSMENU = &H80000

Private Declare Function _
FindWindow Lib "User32" Alias "FindWindowA" ( _
ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long
Private Declare Function _
GetWindowLong Lib "User32" Alias "GetWindowLongA" ( _
ByVal hWnd As Long, ByVal nIndex As Long) As Long
Private Declare Function _
SetWindowLong Lib "User32" Alias "SetWindowLongA" ( _
ByVal hWnd As Long, ByVal nIndex As Long, _
ByVal dwNewLong As Long) As Long
Private Declare Function _
DrawMenuBar Lib "User32" (ByVal hWnd As Long) As Long

' now place this in:

Private Sub UserForm_Initialize()

xl_hwnd = FindWindow(vbNullString, Me.Caption)
If xl_hwnd <> 0 Then
lStyle = GetWindowLong(xl_hwnd, GWL_STYLE)
lStyle = SetWindowLong(xl_hwnd, GWL_STYLE, lStyle And Not WS_SYSMENU)
DrawMenuBar xl_hwnd
End If

End Sub

If it doesn't function let me know as it works fine on my end and maybe I just forgot a piece of it when I posted this.

Perry
 
Upvote 0

Forum statistics

Threads
1,214,793
Messages
6,121,619
Members
449,039
Latest member
Mbone Mathonsi

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