How to prevent window close in word

Krishna M
Krishna M used Ask the Experts™
on
Hi all,

I want to control the document by user closing document using "X"(close active window).
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Fabrice LambertConsulting
Distinguished Expert 2017

Commented:
Hi,

Afraid it isn't doable.
There is no point doing this anyway, as logging out from Windows or turning the computer off will bypass this.

@Zakaria: OP is speaking about Word.
Zakaria AcharkiAnalyst Developer
Distinguished Expert 2018

Commented:
Take a look at the following comment :

https://www.experts-exchange.com/questions/24549702/Disable-Word-Close-Button-Programmatically-in-VB-Net.html#a24851521

That may help if you can't find a way to prevent the closure.
Krishna MTechnical Associat

Author

Commented:
Actually, what I need is if user close the word document, I will do some quick checks in doc and alert the user prevent close the document without correct those things.  I used to control word closing using WordCommands("FileClose" and "FileCloseOrExit") but when user use "X" button to close the document it called Window close and I don't know how to control that.
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Krishna MTechnical Associat

Author

Commented:
I don't want to Disable the "X" button but I just want to do some quick check if user click that "X" button.
Fabrice LambertConsulting
Distinguished Expert 2017

Commented:
There is a Document_Close event, but it does not offer the possibility to be canceled.

Best you can do is warn the user, and maybe set a custom property indicating the document checking failed (for later re-use).
After all, it is the user's responsibility to ensure the document is correct, you code is just a validation tool.
Krishna MTechnical Associat

Author

Commented:
I can't use Document_Close event, because I am using .dotm file and user will use multi document. I should tell them to do that process and if not it will impact critical issue with clients. So, as a developer I should validate and not allow them to close the word. I handled with WordCommands("FileClose" and "FileCloseOrExit") but If user clicks "X" button and it controled by window object so I don't know how to control window.
Fabrice LambertConsulting
Distinguished Expert 2017

Commented:
I should validate
You said it: Validate.

Warn users, if they still save incorrect document, it isn't your responsibility.
Code can alter the document's behavior.
Code can not alter user's behavior.
Krishna MTechnical Associat

Author

Commented:
It's what ever!!!!!!!!!! But, I need to do something when click the "X" button. My request is simply, if user click the "X" button in word window then I need to run some code before closing the word document. Is there any possibilities?
Technical Associat
Commented:
This what I expected from you all. Finally, I found the solution.

Public X As EventClassModule

Sub AutoExec()
    Set X = New EventClassModule
    Set X.pApp = Word.Application
End Sub

Open in new window


Public WithEvents pApp As Word.Application

Private Sub pApp_DocumentBeforeClose(ByVal Doc As Document, Cancel As Boolean)
....... put some code here..........
End Sub

Open in new window

Krishna MTechnical Associat

Author

Commented:
Thank you all.
Zakaria AcharkiAnalyst Developer
Distinguished Expert 2018

Commented:
You're welcome, Glad we could help.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial