MonteDelMar
asked on
Word 2010 - Prevent user from closing application by clicking on Windows X button
Please can you tell me how to disable the Windows X button so as to prevent users closing Word in this way? I have searched and searched but not yet found a solution that works (or perhaps that I understand!).
Many thanks
Archie
Many thanks
Archie
ASKER
Hi Jean-Marc
Thank you for your reply. Does this code refer to the application or the document? I am looking for something to prevent the application closing when the user clicks X on the application window, rather than the document window.
Regards
Thank you for your reply. Does this code refer to the application or the document? I am looking for something to prevent the application closing when the user clicks X on the application window, rather than the document window.
Regards
This is really on the Document.
I don't think it's possible to prevent the application itself to never close.
May I ask why you would like Word to never close? every word file opened would always stay opened.
I don't think it's possible to prevent the application itself to never close.
May I ask why you would like Word to never close? every word file opened would always stay opened.
ASKER
Previously, a new Word document was initiated via another application. Users would complete the document and then have to close Word (ie not just the document) to complete the process. They are used to clicking the X to close the application.
Due to recent changes, they should no longer quit Word entirely. Whilst they still initiate documents via the application previously referred to, they should close the document but leave Word running. Force of habit means they are continuing to close Word, and doing so by clicking on the X, which causes problems elsewhere.
When they actually want to close Word entirely, they can do so by clicking File, Exit.
Due to recent changes, they should no longer quit Word entirely. Whilst they still initiate documents via the application previously referred to, they should close the document but leave Word running. Force of habit means they are continuing to close Word, and doing so by clicking on the X, which causes problems elsewhere.
When they actually want to close Word entirely, they can do so by clicking File, Exit.
So if i understand, you are looking for the Ctrl+F4 function correct?
ASKER
Ctrl+F4 will close the document, won't it? I am wanting to stop the user clicking the X to close Word - as highlighted in the attached jpg. I think it will involve the Windows API but not sure beyond that.
X-button.jpg
X-button.jpg
Sorry, try this: Ctrl+W
ASKER
Thanks but Ctrl+W closes the document. I am not looking for a different way to close the document - just a way to prevent the X from quitting the application.
This would be the max i can do for you.
Let me know if this can work.
Let me know if this can work.
Dim WithEvents myApp As Application
Private Sub Document_Open()
Set myApp = Me.Application
End Sub
Private Sub myApp_DocumentBeforeClose(ByVal Doc As Document, Cancel As Boolean)
Me.Close (Word.WdSaveOptions.wdDoNotSaveChanges)
End Sub
If you want to save before closing the doc, you can do:
Dim WithEvents myApp As Application
Private Sub Document_Open()
Set myApp = Me.Application
End Sub
Private Sub myApp_DocumentBeforeClose(ByVal Doc As Document, Cancel As Boolean)
Me.Close (Word.WdSaveOptions.wdSaveChanges)
End Sub
ASKER
Thank you for your efforts, Jean-Marc, but this is all seems to be related to closing the document rather than the application.
Isn't that what you requested?
Whilst they still initiate documents via the application previously referred to, they should close the document but leave Word running.
ASKER
No, what I am looking for is 'how to disable the Windows X button so as to prevent users closing Word in this way', ie the Word application rather than a document. The question specifically relates to the X method of closing the application.
You can't prevent Word application to never close. Not that i know.
Sorry
Sorry
ASKER
I think I have found it at http://windowssecrets.com/forums/showthread.php/52259-System-Control-Close-%28Word-2002%29.
Pop this in a VBA module:
Option Explicit
Private Declare Function GetSystemMenu Lib "user32" _
(ByVal hwnd As Long, ByVal bRevert As Long) As Long
Private Declare Function GetMenuItemCount Lib "user32" _
(ByVal hMenu As Long) As Long
Private Declare Function DrawMenuBar Lib "user32" _
(ByVal hwnd As Long) As Long
Private Declare Function RemoveMenu Lib "user32" _
(ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long) As Long
Declare Function FindWindow32 Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Const MF_BYPOSITION = &H400&
Const MF_REMOVE = &H1000&
Sub AutoExec() ' Remove System Control Close "X"
'MsgBox "Ready to run."
Dim hSysMenu As Long, nCnt As Long
Dim hWndWD As Long
' Get handle to our form's system menu
' (Restore, Maximize, Move, close etc.)
hWndWD = FindWindow32("OpusApp", vbNullString)
hSysMenu = GetSystemMenu(hWndWD, False)
If hSysMenu Then
' Get System menu's menu count
nCnt = GetMenuItemCount(hSysMenu)
If nCnt Then
' Menu count is based on 0 (0, 1, 2, 3...)
RemoveMenu hSysMenu, nCnt - 1, MF_BYPOSITION Or MF_REMOVE
' RemoveMenu hSysMenu, nCnt - 2, MF_BYPOSITION Or MF_REMOVE ' Remove the seperator
DrawMenuBar hWndWD
End If
End If
End Sub
Pop this in a VBA module:
Option Explicit
Private Declare Function GetSystemMenu Lib "user32" _
(ByVal hwnd As Long, ByVal bRevert As Long) As Long
Private Declare Function GetMenuItemCount Lib "user32" _
(ByVal hMenu As Long) As Long
Private Declare Function DrawMenuBar Lib "user32" _
(ByVal hwnd As Long) As Long
Private Declare Function RemoveMenu Lib "user32" _
(ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long) As Long
Declare Function FindWindow32 Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Const MF_BYPOSITION = &H400&
Const MF_REMOVE = &H1000&
Sub AutoExec() ' Remove System Control Close "X"
'MsgBox "Ready to run."
Dim hSysMenu As Long, nCnt As Long
Dim hWndWD As Long
' Get handle to our form's system menu
' (Restore, Maximize, Move, close etc.)
hWndWD = FindWindow32("OpusApp", vbNullString)
hSysMenu = GetSystemMenu(hWndWD, False)
If hSysMenu Then
' Get System menu's menu count
nCnt = GetMenuItemCount(hSysMenu)
If nCnt Then
' Menu count is based on 0 (0, 1, 2, 3...)
RemoveMenu hSysMenu, nCnt - 1, MF_BYPOSITION Or MF_REMOVE
' RemoveMenu hSysMenu, nCnt - 2, MF_BYPOSITION Or MF_REMOVE ' Remove the seperator
DrawMenuBar hWndWD
End If
End If
End Sub
When i look at the code, look like this is under a Word Doc and not Word Application.
The code will remove the X from the specific doc only and not all word application doc.
The code will remove the X from the specific doc only and not all word application doc.
ASKER
I am running it in an AutoExec macro in a template that is automatically loaded in the Startup folder. It disables the X for the application but not the Word document.
I have just tried the above code on Word 2013 and my below code does the exact same thing. The X is just disabled.
It also just apply on the specific Document with the macro and not the entire Word application new instance.
But i can't test it on Word 2010.
On Word 2013, you don't have the second X for Documents.
It also just apply on the specific Document with the macro and not the entire Word application new instance.
But i can't test it on Word 2010.
On Word 2013, you don't have the second X for Documents.
Dim WithEvents myApp As Application
Private Sub Document_Open()
Set myApp = Me.Application
End Sub
Private Sub myApp_DocumentBeforeClose(ByVal Doc As Document, Cancel As Boolean)
If Doc.Name = Me.Name Then Cancel = True
End Sub
ASKER
That's interesting. I am working in 2010 at the moment. Is that affected by the Windows in Taskbar setting? I have that un-checked.
ASKER
OK, thanks Jean-Marc. I will take a look at 2013. One step forwards ... !
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I believe that is to do with the Windows in Taskbar setting (Options, Advanced, Display, Show all Windows in Taskbar) which was in 2010 but seems to have been dropped from 2013 (which won't be popular).
correct.
ASKER
That it's WiT or that it won't be popular? :-)
This is probably what you are looking for.
The only way to close it will be to use the task manager and do an End Task on that Word File.
Just remember that if the Macro's are not activated, they will be able to close it.
Open in new window