• Status: Solved
  • Priority: Medium
  • Security: Private
  • Views: 28
  • Last Modified:

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
0
MonteDelMar
Asked:
MonteDelMar
  • 13
  • 12
1 Solution
 
Wilder1626Commented:
Hi

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.

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

Open in new window

0
 
MonteDelMarAuthor Commented:
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
0
 
Wilder1626Commented:
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.
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
MonteDelMarAuthor Commented:
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.
0
 
Wilder1626Commented:
So if i understand, you are looking for the Ctrl+F4 function correct?
0
 
MonteDelMarAuthor Commented:
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
0
 
Wilder1626Commented:
Sorry, try this: Ctrl+W
0
 
MonteDelMarAuthor Commented:
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.
0
 
Wilder1626Commented:
This would be the max i can do for you.
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

Open in new window

0
 
Wilder1626Commented:
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

Open in new window

0
 
MonteDelMarAuthor Commented:
Thank you for your efforts, Jean-Marc, but this is all seems to be related to closing the document rather than the application.
0
 
Wilder1626Commented:
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.
0
 
MonteDelMarAuthor Commented:
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.
0
 
Wilder1626Commented:
You can't prevent Word application to never close. Not that i know.

Sorry
0
 
MonteDelMarAuthor Commented:
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
0
 
Wilder1626Commented:
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.
0
 
MonteDelMarAuthor Commented:
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.
0
 
Wilder1626Commented:
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.

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

Open in new window

0
 
MonteDelMarAuthor Commented:
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.
0
 
Wilder1626Commented:
Here is the view you should get in Word 2013.

I little different on the options also in 2013.

Word task bare
0
 
MonteDelMarAuthor Commented:
OK, thanks Jean-Marc.  I will take a look at 2013.  One step forwards ... !
0
 
Wilder1626Commented:
The difference i may see is that when Word is open and you open a new doc, it also open another window. Look's like 2010 is the same window for both. Does it?
0
 
MonteDelMarAuthor Commented:
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).
0
 
Wilder1626Commented:
correct.
0
 
MonteDelMarAuthor Commented:
That it's WiT or that it won't be popular? :-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

  • 13
  • 12
Tackle projects and never again get stuck behind a technical roadblock.
Join Now