Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Disable X (close) button in window of form in VBA

Status
Not open for further replies.

be17

Technical User
Feb 7, 2001
26
0
0
US
I know that in Visual Basic you can use the ControlBox property to remove the min,max, and close buttons from the window. However, I am working with VBA in word and have created several forms. The ControlBox property is not available in these userforms. Does anyone know of any way to remove the X button from the forms?
 
I don't know if what I have will work with VBA, but I have a routine that will disable the X (Close Button), and disable the Close feature on the system menu that works just fine in Visual Basic. If you want to try it and see if it works with your VBA project, drop me a note a rmggray@hotmail.com and I'll send it to you. It requires 5 separate API calls which use a UDT (User Define Type), and some constants.
 
Try this but substitute the form name on the Call for the form U want to disable the "Close" on.


Private Declare Function GetSystemMenu Lib "user32" (ByVal hwnd _
As Long, ByVal bRevert As Boolean) As Long

Private Declare Function GetMenuItemCount Lib "user32" (ByVal _
hMenu As Long) As Long

Private Declare Function RemoveMenu Lib "user32" (ByVal _
hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long) _
As Long

Private Declare Function DrawMenuBar Lib "user32" _
(ByVal hwnd As Long) As Long

Private Const MF_BYPOSITION = &H400&
Private Const MF_REMOVE = &H1000&




Private Sub MDIForm_Load()
Call DisableClose(MDIForm1, True)
End Sub
Public Sub DisableClose(frm As Form, Optional _
Disable As Boolean = True)
'Setting Disable to False disables the 'X',
'otherwise, it's reset

Dim hMenu As Long
Dim nCount As Long

If Disable Then
hMenu = GetSystemMenu(frm.hwnd, False)
nCount = GetMenuItemCount(hMenu)

Call RemoveMenu(hMenu, nCount - 1, MF_REMOVE Or _
MF_BYPOSITION)
Call RemoveMenu(hMenu, nCount - 2, MF_REMOVE Or _
MF_BYPOSITION)

DrawMenuBar frm.hwnd
Else
GetSystemMenu frm.hwnd, True

DrawMenuBar frm.hwnd
End If

End Sub



hope this helps

Adam
 
if you don't want to go the API route...
The 'X' would still be there but clicking on it would not unload the form

Code:
Option Explicit

Private Sub CommandButton1_Click()
  Unload Me
End Sub

Private Sub UserForm_QueryClose(Cancel As Integer, _
                                CloseMode As Integer)
  Cancel = (CloseMode = 0)
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top