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 Westi 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 on excel

Status
Not open for further replies.

riteoh1

Technical User
Aug 19, 2009
1
AU
I am trying to find a way of disabling the x (close) button on an excel worksheet, and excel program. I have googled a way to disable it on a user form, but I want to disable the close button on a worksheet, via vba.
Can anybody provide me with a way to do this?

Thanks.
 
You should be able to. I needed to disable and re-enable the app minimize button of Excel while collecting data from the comm port. I seem to recall that I got this snippet from a helpful tek-tips soul on the visual basic 5/6 forum. I know its not what you asked for but it may point you in the right direction.
Greg

Code:
Type MENUITEMINFO
  cbSize As Long
  fMask As Long
  fType As Long
  fState As Long
  wID As Long
  hSubMenu As Long
  hbmpChecked As Long
  hbmpUnchecked As Long
  dwItemData As Long
  dwTypeData As String
  cch As Long
End Type

'Menu item constants.
Const SC_MINIMIZE As Long = &HF020&
Const xSC_MINIMIZE As Long = -10

'SetMenuItemInfo fState constants.
Const MFS_GRAYED As Long = &H3&
Const MFS_DEFAULT As Long = &H1000&

'SetMenuItemInfo fMask constants.
Const MIIM_STATE As Long = &H1&
Const MIIM_ID As Long = &H2&

'SendMessage constants.
Const WM_NCACTIVATE  As Long = &H86

'Window constants
Const WS_MINIMIZEBOX As Long = &H20000
Const WS_MAXIMIZEBOX As Long = &H10000
Const GWL_STYLE As Long = (-16)



Declare Function SetMenuItemInfo Lib "user32" Alias "SetMenuItemInfoA" (ByVal hMenu As Long, ByVal un As Long, ByVal bool As Boolean, lpcMenuItemInfo As MENUITEMINFO) As Long
Declare Function GetDesktopWindow Lib "user32" () As Long
Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Declare Function GetCurrentProcessId Lib "kernel32" () As Long
Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, ByRef lpdwProcessId As Long) As Long
Declare Function GetSystemMenu Lib "user32.dll" (ByVal hwnd As Long, ByVal bRevert As Long) As Long
Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long


Sub DisableAppMinimize()
Dim hWndExcel As Long
Dim hSysMenu As Long
Dim retVal As Long
Dim MI_Info As MENUITEMINFO
    
   hWndExcel = GetWindowHandle("XLMAIN", Application.Caption)
   hSysMenu = GetSystemMenu(hWndExcel, 0)
         
   With MI_Info
     .cbSize = Len(MI_Info)
     .fState = MFS_GRAYED
     .wID = xSC_MINIMIZE
     .fMask = MIIM_ID Or MIIM_STATE
   End With
   retVal = SetMenuItemInfo(hSysMenu, SC_MINIMIZE, False, MI_Info)

   retVal = GetWindowLong(hWndExcel, GWL_STYLE)
   retVal = retVal And Not (WS_MINIMIZEBOX)
   retVal = SetWindowLong(hWndExcel, GWL_STYLE, retVal)
   
   retVal = SendMessage(hWndExcel, WM_NCACTIVATE, True, 0)
   
End Sub


Sub EnableAppMinimize()
Dim hWndExcel As Long
Dim hSysMenu As Long
Dim retVal As Long
Dim MI_Info As MENUITEMINFO

   hWndExcel = GetWindowHandle("XLMAIN", Application.Caption)
   hSysMenu = GetSystemMenu(hWndExcel, 0)
         
   With MI_Info
     .cbSize = Len(MI_Info)
     .fState = MFS_DEFAULT
     .wID = SC_MINIMIZE
     .fMask = MIIM_ID Or MIIM_STATE
   End With
   retVal = SetMenuItemInfo(hSysMenu, xSC_MINIMIZE, False, MI_Info)

   retVal = GetWindowLong(hWndExcel, GWL_STYLE)
   retVal = SetWindowLong(hWndExcel, GWL_STYLE, WS_MINIMIZEBOX Or retVal)
      
   retVal = SendMessage(hWndExcel, WM_NCACTIVATE, True, 0)
      
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top