J Dont forget to reset xButton Enabied True toreenable the X button o

Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" _

(ByVal lpClassName As String, ByVal lpWindowName As String) As Long Public Declare Function GetSystemMenu Lib "user32" _

(ByVal hWnd As Long, ByVal bRevert As Long) As Long Public Declare Function DeleteMenu Lib "user32" (ByVal hMenu As Long, _

ByVal nPosition As Long, ByVal wFlags As Long) As Long Private Const SC_CLOSE As Long = &HF060

Public Sub XButtonEnabled(ByVal bEnabled As Boolean) Dim hWndForm As Long Dim hMenu As Long hWndForm = FindWindow("XLMAIN", Application.Caption) hMenu = GetSystemMenu(hWndForm, bEnabled) DeleteMenu hMenu, SC_CLOSE, 0&

End Sub

Public Sub ExcelXButton() XButtonEnabled False

If you look carefully at these API declarations, you'll notice that the first call uses an alias, but the next two don't. If you don't want to use an alias, then the function or procedure name must be the same name used in the DLL. In that case, the function name is automatically removed when the function is copied into a VBA module.

After using the ExcelXButton macro, the X button in the top corner of the Excel workbook is actually grayed out as shown in Figure 22.5.

Figure 22.5

Disable the Excel X button to prevent the user from using that method to shut down the application.

0 0

Post a comment