Prevent User Closing Word by clicking on X

  • Thread starter Monty via OfficeKB.com
  • Start date
M

Monty via OfficeKB.com

Hi there

Wonder if anyone has actually achieved preventing the user from closing Word
by clicking on the application's X in the top right corner of the screen? I
have found similar threads but not the answer.

Have tried the Application Quit event but although it runs whatever code I
put there it just goes ahead and quits anyway.

Have also suggested using 3rd party products to do so but this is not
acceptable to the organisation.

Does anyone have the answer?

Thanks in advance.
 
J

Jay Freedman

Monty said:
Hi there

Wonder if anyone has actually achieved preventing the user from
closing Word by clicking on the application's X in the top right
corner of the screen? I have found similar threads but not the
answer.

Have tried the Application Quit event but although it runs whatever
code I put there it just goes ahead and quits anyway.

Have also suggested using 3rd party products to do so but this is not
acceptable to the organisation.

Does anyone have the answer?

Thanks in advance.

Instead of trapping the Quit event, which is already too late, trap the
DocumentBeforeClose event. In that code, check whether the document being
closed is the only open document. If it is, then set the Cancel parameter to
True as shown in the example code in the Help topic.

See
http://word.mvps.org/faqs/macrosvba/AppClassEvents.htm
for more details.
 
C

Chuck Henrich

The following sub DisableWordXButton will disable Word's X button if you call
it from an AutoExec macro. The code below is very slightly modified from
code on the MVPS site and I hope it's ok with those folks to modify and post
it here (see the original at
http://word.mvps.org/faqs/userforms/DisableClose.htm).

Private Const MF_BYPOSITION = &H400
Private Const MF_REMOVE = &H1000

Private Declare Function DrawMenuBar Lib "user32" _
(ByVal hWnd As Long) As Long

Private Declare Function GetMenuItemCount Lib "user32" _
(ByVal hMenu As Long) As Long

Private Declare Function GetSystemMenu Lib "user32" _
(ByVal hWnd As Long, _
ByVal bRevert As Long) As Long

Private Declare Function RemoveMenu Lib "user32" _
(ByVal hMenu As Long, _
ByVal nPosition As Long, _
ByVal wFlags As Long) As Long

Private Declare Function FindWindow Lib "user32" _
Alias "FindWindowA" _
(ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long

Private hWnd As Long

Sub DisableWordXButton()

Dim hMenu As Long
Dim menuItemCount As Long

'Obtain the window handle to the userform
hWnd = FindWindow(vbNullString, "Microsoft Word")

'Obtain the handle to the form's system menu
hMenu = GetSystemMenu(hWnd, 0)

If hMenu Then

'Obtain the number of items in the menu
menuItemCount = GetMenuItemCount(hMenu)

'Remove the system menu Close menu item.
'The menu item is 0-based, so the last
'item on the menu is menuItemCount - 1
Call RemoveMenu(hMenu, menuItemCount - 1, _
MF_REMOVE Or MF_BYPOSITION)

'Remove the system menu separator line
Call RemoveMenu(hMenu, menuItemCount - 2, _
MF_REMOVE Or MF_BYPOSITION)

'Force a redraw of the menu. This
'refreshes the titlebar, dimming the X
Call DrawMenuBar(hWnd)

End If

End Sub
 
M

Monty via OfficeKB.com

Thank you very much. I will try both your solution and Chuck's. What a
great help.

Jay said:
[quoted text clipped - 12 lines]
Thanks in advance.

Instead of trapping the Quit event, which is already too late, trap the
DocumentBeforeClose event. In that code, check whether the document being
closed is the only open document. If it is, then set the Cancel parameter to
True as shown in the example code in the Help topic.

See
http://word.mvps.org/faqs/macrosvba/AppClassEvents.htm
for more details.
 
M

Monty via OfficeKB.com

Thank you both very much.

Chuck said:
The following sub DisableWordXButton will disable Word's X button if you call
it from an AutoExec macro. The code below is very slightly modified from
code on the MVPS site and I hope it's ok with those folks to modify and post
it here (see the original at
http://word.mvps.org/faqs/userforms/DisableClose.htm).

Private Const MF_BYPOSITION = &H400
Private Const MF_REMOVE = &H1000

Private Declare Function DrawMenuBar Lib "user32" _
(ByVal hWnd As Long) As Long

Private Declare Function GetMenuItemCount Lib "user32" _
(ByVal hMenu As Long) As Long

Private Declare Function GetSystemMenu Lib "user32" _
(ByVal hWnd As Long, _
ByVal bRevert As Long) As Long

Private Declare Function RemoveMenu Lib "user32" _
(ByVal hMenu As Long, _
ByVal nPosition As Long, _
ByVal wFlags As Long) As Long

Private Declare Function FindWindow Lib "user32" _
Alias "FindWindowA" _
(ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long

Private hWnd As Long

Sub DisableWordXButton()

Dim hMenu As Long
Dim menuItemCount As Long

'Obtain the window handle to the userform
hWnd = FindWindow(vbNullString, "Microsoft Word")

'Obtain the handle to the form's system menu
hMenu = GetSystemMenu(hWnd, 0)

If hMenu Then

'Obtain the number of items in the menu
menuItemCount = GetMenuItemCount(hMenu)

'Remove the system menu Close menu item.
'The menu item is 0-based, so the last
'item on the menu is menuItemCount - 1
Call RemoveMenu(hMenu, menuItemCount - 1, _
MF_REMOVE Or MF_BYPOSITION)

'Remove the system menu separator line
Call RemoveMenu(hMenu, menuItemCount - 2, _
MF_REMOVE Or MF_BYPOSITION)

'Force a redraw of the menu. This
'refreshes the titlebar, dimming the X
Call DrawMenuBar(hWnd)

End If

End Sub
[quoted text clipped - 11 lines]
Thanks in advance.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top