Userform close button

Skyhook

New Member
Joined
Apr 8, 2002
Messages
38
Is There any way to disable the Close “X” button on a userform.

Thanks in advance
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
This code will disable the close button and will actually "grey" it out to show that's disabled:

<pre>
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetSystemMenu Lib "user32" (ByVal hWnd As Long, ByVal bRevert As Long) As Long
Private Declare Function DeleteMenu Lib "user32" (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long) As Long

Private Const SC_CLOSE As Long = &HF060

Private Sub UserForm_Initialize()
Dim hWndForm As Long
Dim hMenu As Long

If Val(Application.Version) < 9 Then
hWndForm = FindWindow("ThunderXFrame", Me.Caption) 'XL97
Else
hWndForm = FindWindow("ThunderDFrame", Me.Caption) 'XL2000
End If

'Grey out Close button
hMenu = GetSystemMenu(hWndForm, 0)
DeleteMenu hMenu, SC_CLOSE, 0&
End Sub</pre>

HTH
 
Upvote 0
Thanks for the quick reponse. This has got to be the best board around. Hope one day to be able to help.
 
Upvote 0
Mark, WHERE do I have to place your code. I don't understand the meaning of UserForm. Do I have to use a button? I want also to grey the x-button but only when a condition is true. How do I have to do this?
 
Upvote 0
Gandalf, welcome to the board.

If you don't know what a Userform is, then I'm assuming that you want to grey out one of the "X" buttons in Excel itself? Either for the Workbook, or for the Excel application?

Is this correct?
 
Upvote 0
To disable the close button in Excel you can use this code plus the "test" procedure to check it:

<pre>
Option Explicit

Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public Declare Function GetSystemMenu Lib "user32" (ByVal hWnd As Long, ByVal bRevert As Long) As Long
Public Declare Function DeleteMenu Lib "user32" (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long) As Long

Private Const SC_CLOSE As Long = &HF060

Public Sub XBoxEnabled(ByVal bEnabled As Boolean)
Dim hWndForm As Long
Dim hMenu As Long

hWndForm = FindWindow("XLMAIN", Application.Caption)

hMenu = GetSystemMenu(hWndForm, bEnabled)
DeleteMenu hMenu, SC_CLOSE, 0&
End Sub

Public Sub test()

XBoxEnabled True

End Sub</pre>

As soon as someone tells me what the Classname is for a worksheet, we can disable that little bugger as well.

HTH
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,685
Members
448,977
Latest member
dbonilla0331

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