Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2010-09-14
2
Medium Priority
?
407 Views
Last Modified: 2012-05-10
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

0
Comment
Question by:wcpoa8
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 9

Expert Comment

by:hitsdoshi1
ID: 33676670
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.
0
 

Accepted Solution

by:
wcpoa8 earned 0 total points
ID: 33676686
This code works after all. I have my file wrapped in software called LockXLS. That seems to be the problem. Sorry about that. I need to close this question.
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This article describes a serious pitfall that can happen when deleting shapes using VBA.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question