Hello Fredriksson,
Add a Standard Module to your VBA project and then copy and paste the code below into it. There's a lot of code, but the macro is very easy to use. Place the macro call before you call the dialog.
Example:
Call DisableCloseBox(250)
Application.Dialogs(xlDialogOpen).Show
The 250 is a time delay of 1/4 second (250 milliseconds). This has worked well for me. If you need to increase the time, you can.
'//////////////////////////////////'
'/ /'
'/ Disable Dialog Close Box Macro /'
'/ Written Jan. 26, 2007 /'
'/ Author: Leith Ross /'
'/ /'
'//////////////////////////////////'
'Returns the Window Handle of the Window
'that is accepting User input.
Public Declare Function GetForegroundWindow _
Lib "user32.dll" _
() As Long
'Retuns the Handle to the System Menu
Private Declare Function GetSystemMenu _
Lib "user32.dll" _
(ByVal hwnd As Long, _
ByVal bRevert As Long) As Long
'Returns the number Menu Items in the System Menu
Private Declare Function GetMenuItemCount _
Lib "user32.dll" _
(ByVal hMenu As Long) As Long
'Allows Items to be disabled or removed from the System Menu
Private Declare Function RemoveMenu _
Lib "user32.dll" _
(ByVal hMenu As Long, _
ByVal nPosition As Long, _
ByVal wFlags As Long) As Long
'Redraw the Icons on the Window's Title Bar
Private Declare Function DrawMenuBar _
Lib "user32.dll" _
(ByVal hwnd As Long) As Long
Private Declare Function SetTimer _
Lib "user32.dll" _
(ByVal hwnd As Long, _
ByVal nIDevent As Long, _
ByVal uElapse As Long, _
ByVal lpTimerfunc As Long) As Long
Private Declare Function KillTimer _
Lib "user32.dll" _
(ByVal hwnd As Long, _
ByVal nIDevent As Long) As Long
Private Declare Function GetTickCount _
Lib "kernel32.dll" _
() As Long
'Menu Constants
Const MF_BYPOSITION As Long = &H400
Const MF_DISABLED As Long = &H2
Const MF_GRAYED As Long = &H1
Const MF_ENABLED As Long = &H0
'==========Public Timer Variable Declarations ==============================
Public TimerStartTime As Long
Public TimerID As Long 'Turn On and Off with this ID
Public TimerActive As Boolean 'Is the timer active
'============================================================
Public Sub TurnTimerOn(ByVal MilliSec As Long)
If TimerActive Then Call TurnTimerOff
On Error Resume Next
TimerID = SetTimer(0, 0, MilliSec, AddressOf TimerProc)
TimerActive = True
End Sub
Public Sub TurnTimerOff()
KillTimer 0, TimerID
End Sub
Private Sub TimerProc(ByVal hwnd As Long, ByVal uMsg As Long, ByVal nIDevent As Long, ByVal dwTime As Long)
'Disable the Close Box
hwnd = GetForegroundWindow()
hMenu = GetSystemMenu(hwnd, Enabled)
MIC = GetMenuItemCount(hMenu)
Call RemoveMenu(hMenu, MIC - 1, MF_DISABLED Or MF_BYPOSITION)
Call DrawMenuBar(hwnd)
TurnTimerOff
End Sub
Public Sub DisableCloseBox(ByVal TimeDelay As Long)
'Time Delay in milliseconds. 1 millisecond = 1/1000 of a second
TurnTimerOn TimeDelay
End Sub
Sincerely,
Leith Ross
Bookmarks