Disable X

M

Mauricio

Hi all,
is there any way to hide or disable X button (at the top right of the
window) to avoid a user to close Word?
Thanks in advance.

Mauricio
Barcelona, Spain
 
L

LtCommander

You can disable the X button from the QueryClose method:

Example:

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As
Integer)
Cancel = 1

End Sub


HTH,
Vince
 
J

Jean-Guy Marcil

LtCommander was telling us:
LtCommander nous racontait que :
You can disable the X button from the QueryClose method:

Example:

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As
Integer)
Cancel = 1

End Sub
Make sure you tell the user what is going on, I would hate to click on the
X, expect the form to close, but instead, nothing happens, click again,
nothing again...

For example:

If CloseMode = vbFormControlMenu Then
MsgBox "You cant quit like that!"
Cancel = True
End If

Or tell them to use your own buttons instead.

Personally, I prefer to disable it, so visually, the user knows the X is not
functional.
See:
http://word.mvps.org/FAQs/Userforms/DisableClose.htm


--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
G

Greg

LtCdr/JGM

Which userform are you advising the OP to put any to this code in? He
wasn't to disable the X that closes Word. Where does he find the Word
userform?
 
J

Jean-Guy Marcil

Greg was telling us:
Greg nous racontait que :
LtCdr/JGM

Which userform are you advising the OP to put any to this code in? He
wasn't to disable the X that closes Word. Where does he find the Word
userform?

Oooooopsss!
Beat me with a stick!

I am guilty of "readingtoofastisitis" I read LtCdr response, but sorta
skimmed over the OP post...

Sorry, as far as I know, you cannot disable the X on the Word window itself.

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
J

Jonathan West

Jean-Guy Marcil said:
Greg was telling us:
Greg nous racontait que :


Oooooopsss!
Beat me with a stick!

I am guilty of "readingtoofastisitis" I read LtCdr response, but sorta
skimmed over the OP post...

Sorry, as far as I know, you cannot disable the X on the Word window
itself.


You can disable the X, using similar techniques as for disabling the X in a
UserForm, there are just different techniques needed for identifying the
window handle for that document window.

But I think that it is hardly worth doing - there are so many ways that a
determined user can close Word that absolutely preventing it is not
possible.

--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
J

Jean-Guy Marcil

Jonathan West was telling us:
Jonathan West nous racontait que :
You can disable the X, using similar techniques as for disabling the
X in a UserForm, there are just different techniques needed for
identifying the window handle for that document window.

I meant through VBA...
Do you mean that there is a VBA way of doing this?
I thought it was only possible through somewhat complicated API calls.
But I think that it is hardly worth doing - there are so many ways
that a determined user can close Word that absolutely preventing it
is not possible.

Very true!

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
B

Bo Hansson

I assume that you have your own Close button with associated code on the
user form..
Then you could use the forms Terminate event to trigger your own button:
cmdClose_Click

/BosseH
 
J

Jonathan West

I meant through VBA...
Do you mean that there is a VBA way of doing this?
I thought it was only possible through somewhat complicated API calls.

Depends what you think of as complicated. Most of the code in this article
can be used

How to disable the X close button on a UserForm
http://word.mvps.org/FAQs/Userforms/DisableClose.htm

The key difference is that you need to get the window handle of the document
window, and use that rather than the window handle for the UserForm window.
You can get the window handle of just about any window by using the
FundWindow API call and feeding it all or part of the window caption.


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
J

Jean-Guy Marcil

Jonathan West was telling us:
Jonathan West nous racontait que :
Depends what you think of as complicated. Most of the code in this
article can be used

How to disable the X close button on a UserForm
http://word.mvps.org/FAQs/Userforms/DisableClose.htm

Duh!
Must be a slow day!
Right you are, so the following, by using API stuff from VBA will dim the X
on the Word application Window:

'_______________________________________
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 DisableXinWordWindow()

Dim hMenu As Long
Dim menuItemCount As Long
Dim MyWinCaption As String

MyWinCaption = ActiveWindow.Caption & " - " & Application.Caption

'Obtain the window handle to the userform
hWnd = FindWindow(vbNullString, MyWinCaption)

'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
'_______________________________________

But, as you said, that by itself is useless.

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
G

Greg Maxey

JGM,

No corporal punishment intended or desired (other than a few self-inflicted
blows to myself for my ever deteriorating spelling and grammar) :-(
 
L

LtCommander

Guys,
Sorry! I read the original post too quickly and was immediately on a
train back to HK! Sorry real navy guy (Greg)

Vince
 
M

Mauricio

Bo Hansson escribió:
I assume that you have your own Close button with associated code on the
user form..
Then you could use the forms Terminate event to trigger your own button:
cmdClose_Click

/BosseH
Thanks guys.
In order to explain better what I want to do: there is an applicattion
that communicates with Word through DDE. There are thousands of document
created with a macro inside them. Than a user wants to avoid that theirs
users close Word with the X so I have to make something. My idea was to
disable X but if you have any other idea... Keep on mind that I have to
modified each document created (not at the same time, perhaps every time
that somebody opens the doc).
I will try disabling X, for the moment.
Thanks again.

Mauricio
 

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