call the following routine with
HideCloseButton <formObject>
insert into a module:
'>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
'Find the Dialog's Window
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
'Get the current window style
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
'Set the new window style
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
'Routine to hide the close button on a userform or dialogsheet
' oDialog is either the Userform or Dialog object
Sub HideCloseButton(oDialog As Object)
Dim hWnd As Long, lStyle As Long
'Were we given a userform or a dialog sheet
If TypeName(oDialog) = "DialogSheet" Then
'We had a dialog sheet. Note that pressing Escape still closes the dialog
Select Case Int(Val(Application.Version))
Case 5 'Doesn't work in Excel 5 - we only have 32-bit DLL calls here
Case 7 'Excel 95
hWnd = FindWindow("bosa_sdm_XL", oDialog.DialogFrame.Caption) 'DialogSheet
Case 8 'Excel 97
hWnd = FindWindow("bosa_sdm_XL8", oDialog.DialogFrame.Caption) 'DialogSheet
Case 9 'Excel 2000
hWnd = FindWindow("bosa_sdm_XL9", oDialog.DialogFrame.Caption) 'DialogSheet
End Select
Else
'We had a userform
Select Case Int(Val(Application.Version))
Case 8 'Excel 97
hWnd = FindWindow("ThunderXFrame", oDialog.Caption) 'UserForm
Case 9 'Excel 2000
hWnd = FindWindow("ThunderDFrame", oDialog.Caption) 'UserForm
End Select
End If
'Get the current window style
lStyle = GetWindowLong(hWnd, GWL_STYLE)
'Turn off the System Menu bit
SetWindowLong hWnd, GWL_STYLE, lStyle And Not WS_SYSMENU
means you never close the form , not even with a Cancel or Close button. The Unload Me statement would be intercepted and canceled.
The code posted above does not disable the form from being closed by code, wheras yous does.
Setting Cancel = 1 is the same as Cancel = True, but in your code it happens regardless. Maybe that is what you might want, but could prove cumbersome if you need a tidy shutdown.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.