Link to home
Start Free TrialLog in
Avatar of wcpoa8
wcpoa8

asked on

How can I disable the close "X" in the Excel 2007/2010 title bar?

The code below works in thisWorkbook in Excel 2003. I need a way to do this in Excel 2007/2010. Can anyone help?

Option Explicit

'Written: December 24, 2008
'Author:  Leith Ross
'Summary: Enables or Disables the Close "X" on Excel's title bar.

Private Declare Function FindWindow _
  Lib "user32.dll" _
   Alias "FindWindowA" _
    (ByVal lpszClass As String, _
     ByVal lpszWindow As String) As Long
     
Private Declare Function GetSystemMenu _
  Lib "user32.dll" _
  (ByVal hwnd As Long, _
   ByVal bRevert 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.dll" _
    (ByVal hwnd As Long) As Long

Public Sub SetCloseX(ByVal bEnable As Boolean)

  Dim Action As Long
  Dim hMenu As Long
  Dim hwnd As Long
  Dim Ret As Long
  
  Const MF_BYCOMMAND As Long = &H0
  Const SC_CLOSE As Long = &HF060
  
    Action = CLng(bEnable)
    hwnd = FindWindow("XLMAIN", vbNullString)
    hMenu = GetSystemMenu(hwnd, Action)
    
    Ret = RemoveMenu(hMenu, SC_CLOSE, MF_BYCOMMAND)
    Ret = DrawMenuBar(hwnd)

End Sub

Sub RedXworks()
'put this part into workbook_activate, not behind a button
SetCloseX True
End Sub

Sub RedXdisabled()
'put this part into workbook_deactivate, not behind a button
SetCloseX False
End Sub

Open in new window

Avatar of hitsdoshi1
hitsdoshi1

Please the code in Thisworkbook object module

Private Sub Workbook_BeforeClose(Cancel As Boolean)
If [your criteria] then
MsgBox "Close Diabled"
Cancel = True
endif
End Sub

Cancel = True which will cancel/disable the close operation.
ASKER CERTIFIED SOLUTION
Avatar of wcpoa8
wcpoa8

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial