Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Preventing user from closing form with "X"


Am using the following code to make sure user closes a form correctly

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode A
Integer)
If CloseMode = vbFormControlMenu Then
Cancel = True
MsgBox "You must use the form 'close' button", 64, "Erro
Message"
End If
End Sub

I have 60 userforms. Rather than apply code in each form, is there
way to universally apply this to all forms in the workbook??

Follow up question, is it possible to remove the Microsoft "X" at th
top right hand side of the UserForm??

Any help much appreciated


Peter (new to VBA...slowly getting there, thanks to all the good advic
from this forum!

--
peter.thompso
-----------------------------------------------------------------------
peter.thompson's Profile: http://www.excelforum.com/member.php...fo&userid=2968
View this thread: http://www.excelforum.com/showthread.php?threadid=49610

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 923
Default Preventing user from closing form with "X"

You can create a function in a general module that tests the value passed to
it as if they were in the form, the function returns a True or False
depending on the method used to close the Form. Try this......

In each userform change the Query Close event as follows

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
Cancel = UnloadError(Cancel, CloseMode)
End Sub

In a general module put in this function ......

Function UnloadError(Cancel As Integer, CloseMode As Integer)
UnloadError = False
If CloseMode = vbFormControlMenu Then
UnloadError = True
MsgBox "You must use the form 'close' button", 64, "Error Message"
End If
End Function



If you wish to remove the X you have to remove the Form Control Bar, this
can be achieved using the following.....
This uses windows library calls it needs to be installed in each userform.

In the userform module declaration area (before any modules) ....

'heading for toolbar remover
Private Declare Function FindWindow Lib "user32" Alias _
"FindWindowA" (ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long
Private Declare Function GetWindowLong Lib "user32" Alias _
"GetWindowLongA" (ByVal hWnd As Long, _
ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias _
"SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, _
ByVal dwNewLong As Long) As Long
Private Declare Function DrawMenuBar Lib "user32" ( _
ByVal hWnd As Long) As Long
' ******** end of code

In the userform initialize event place this code ........

Call HideCaptionBar
' ********end of code

In the userform put this function......

Private Function HideCaptionBar()
Dim lngHnd As Long
Dim lngStyle As Long
Dim lngH(1) As Long
Const GWL_STYLE = (-16)
Const WS_CAPTION = &HC00000
lngH(0) = Me.Height - Me.InsideHeight
If Val(Application.Version) 8 Then
lngHnd = FindWindow("ThunderDFrame", Me.Caption)
Else
lngHnd = FindWindow("ThunderXFrame", Me.Caption)
End If
lngStyle = GetWindowLong(lngHnd, GWL_STYLE) And Not WS_CAPTION
SetWindowLong lngHnd, GWL_STYLE, lngStyle
DrawMenuBar lngHnd
lngH(1) = Me.Height - Me.InsideHeight
Me.Height = Me.Height + lngH(1) - lngH(0)
End Function
' ******* end of code



--
Cheers
Nigel



"peter.thompson"
<peter.thompson.20ompm_1135659301.4457@excelforu m-nospam.com wrote in
message news:peter.thompson.20ompm_1135659301.4457@excelfo rum-nospam.com...

Am using the following code to make sure user closes a form correctly

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As
Integer)
If CloseMode = vbFormControlMenu Then
Cancel = True
MsgBox "You must use the form 'close' button", 64, "Error
Message"
End If
End Sub

I have 60 userforms. Rather than apply code in each form, is there a
way to universally apply this to all forms in the workbook??

Follow up question, is it possible to remove the Microsoft "X" at the
top right hand side of the UserForm??

Any help much appreciated


Peter (new to VBA...slowly getting there, thanks to all the good advice
from this forum!)


--
peter.thompson
------------------------------------------------------------------------
peter.thompson's Profile:

http://www.excelforum.com/member.php...o&userid=29686
View this thread: http://www.excelforum.com/showthread...hreadid=496103



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 18
Default Preventing user from closing form with "X"

There is no easy way to disable the close button in an Excel UserForm
to my knowledge. Your approach is the only one I know, besides using an
external dll for the form.
Don't really know about a universal way to apply that to every userform
either :-(.
What you could do is to use VBE:
http://www.cpearson.com/excel/vbe.htm
With that you could programatically alter all Userforms, but most
likely you spend more time figuring out how to do that than doing it
manually..

Cheers
Remy Blaettler
http://www.collaboral.com

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Preventing user from closing form with "X"


Thanks Nigel & Remy - much appreciated

Cheers

Pete

--
peter.thompso
-----------------------------------------------------------------------
peter.thompson's Profile: http://www.excelforum.com/member.php...fo&userid=2968
View this thread: http://www.excelforum.com/showthread.php?threadid=49610

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Preventing "undeleting" data in Excel megsolve Excel Discussion (Misc queries) 2 December 11th 08 07:59 PM
Excel - Golf - how to display "-2" as "2 Under" or "4"as "+4" or "4 Over" in a calculation cell Steve Kay Excel Discussion (Misc queries) 2 August 8th 08 01:54 AM
any formula to convert numbers in word form, e.g. "2" as "Two"? Neeraj Excel Worksheet Functions 1 May 26th 08 01:03 PM
function to return day in the form "Monday", "Tuesday" etc given . MTro Excel Worksheet Functions 2 October 3rd 07 09:49 AM
Put "put user form" in spreadsheet Keith New Users to Excel 3 April 8th 07 07:00 PM


All times are GMT +1. The time now is 07:07 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"