Does anyone know how to disable the red X on the top right of a form?![]()
Does anyone know how to disable the red X on the top right of a form?![]()
Good afternoon jlroper
It can be done, but only through using Windows API calls. It also makes a difference what version of Excel you are using, but the code below should handle this for you. Put this code in your main module:
Option Explicit
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
Sub HideX(UForm As Object)
Dim Win As Long, WinStyle As Long
If Val(Application.Version) >= 9 Then
Win = FindWindow("ThunderDFrame", UForm.Caption)
Else
Win = FindWindow("ThunderXFrame", UForm.Caption)
End If
WinStyle = GetWindowLong(Win, -16)
SetWindowLong Win, -16, WinStyle And Not &H80000
End Sub
Sub DisplayForm()
UserForm1.Show
End Sub
and in your userform module put this in your initialization routine:
HideX UserForm1
Just remember to include a button in there for the user to cancel...!
HTH
DominicB
Dominicb,
When I put in the code:
> Option Explicit
> Private Declare Function FindWindow Lib "user32" Alias "FindWindowA"
> (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
I received an error that said:
Compile error:
Expected: line number or label or statement or end of statement
(the third line above was red also).
Do you know how to get around that (is that the version issue you were
speaking about?)
Thanks.
"dominicb" wrote:
>
> Good afternoon jlroper
>
> It can be done, but only through using Windows API calls. It also
> makes a difference what version of Excel you are using, but the code
> below should handle this for you. Put this code in your main module:
>
> Option Explicit
> 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
>
> Sub HideX(UForm As Object)
> Dim Win As Long, WinStyle As Long
> If Val(Application.Version) >= 9 Then
> Win = FindWindow("ThunderDFrame", UForm.Caption)
> Else
> Win = FindWindow("ThunderXFrame", UForm.Caption)
> End If
> WinStyle = GetWindowLong(Win, -16)
> SetWindowLong Win, -16, WinStyle And Not &H80000
> End Sub
>
> Sub DisplayForm()
> UserForm1.Show
> End Sub
>
> and in your userform module put this in your initialization routine:
>
> HideX UserForm1
>
> Just remember to include a button in there for the user to cancel...!
>
> HTH
>
> DominicB
>
>
> --
> dominicb
> ------------------------------------------------------------------------
> dominicb's Profile: http://www.excelforum.com/member.php...o&userid=18932
> View this thread: http://www.excelforum.com/showthread...hreadid=401423
>
>
I am no expert but try this in the code module for the UserForm
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = vbFormControlMenu Then
Cancel = True
End If
End Sub
"jlroper" wrote:
>
> Does anyone know how to disable the red X on the top right of a form?
>
>
>
> --
> jlroper
> ------------------------------------------------------------------------
> jlroper's Profile: http://www.excelforum.com/member.php...o&userid=26709
> View this thread: http://www.excelforum.com/showthread...hreadid=401423
>
>
Since the user is generally trying to cancel the userform, I execute the
cancel button code from this event. It keeps the user's annoyance level
down.
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = vbFormControlMenu Then
Cancel = True
CancelButton_Click
End If
End Sub
- Jon
-------
Jon Peltier, Microsoft Excel MVP
Peltier Technical Services
Tutorials and Custom Solutions
http://PeltierTech.com/
_______
PraxisPete wrote:
> I am no expert but try this in the code module for the UserForm
> Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
> If CloseMode = vbFormControlMenu Then
> Cancel = True
> End If
> End Sub
>
> "jlroper" wrote:
>
>
>>Does anyone know how to disable the red X on the top right of a form?
>>
>>
>>
>>--
>>jlroper
>>------------------------------------------------------------------------
>>jlroper's Profile: http://www.excelforum.com/member.php...o&userid=26709
>>View this thread: http://www.excelforum.com/showthread...hreadid=401423
>>
>>
Jon,
Yes, I would be annoyed if the form did not act the way it (Windows) is
designed to work.
If it does not function, then it should not be there.
NickHK
"Jon Peltier" <jonREMOVExlmvp@peltierCAPStech.com> wrote in message
news:uORFqnmsFHA.464@TK2MSFTNGP15.phx.gbl...
> Since the user is generally trying to cancel the userform, I execute the
> cancel button code from this event. It keeps the user's annoyance level
> down.
>
> Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
> If CloseMode = vbFormControlMenu Then
> Cancel = True
> CancelButton_Click
> End If
> End Sub
>
> - Jon
> -------
> Jon Peltier, Microsoft Excel MVP
> Peltier Technical Services
> Tutorials and Custom Solutions
> http://PeltierTech.com/
> _______
>
>
> PraxisPete wrote:
> > I am no expert but try this in the code module for the UserForm
> > Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
> > If CloseMode = vbFormControlMenu Then
> > Cancel = True
> > End If
> > End Sub
> >
> > "jlroper" wrote:
> >
> >
> >>Does anyone know how to disable the red X on the top right of a form?
> >>
> >>
> >>
> >>--
> >>jlroper
> >>------------------------------------------------------------------------
> >>jlroper's Profile:
http://www.excelforum.com/member.php...o&userid=26709
> >>View this thread:
http://www.excelforum.com/showthread...hreadid=401423
> >>
> >>
That worked great thanks Mr. Peltier!![]()
Hey Mr. Peltier,
My form is automatically loaded when I click on the icon, is there a way to
incorporate that code you gave me within the "auto_load" module or area
(forgive me if I'm incorrect in stating that it's a module if it is not)?
Thanks.
"Jon Peltier" wrote:
> Since the user is generally trying to cancel the userform, I execute the
> cancel button code from this event. It keeps the user's annoyance level
> down.
>
> Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
> If CloseMode = vbFormControlMenu Then
> Cancel = True
> CancelButton_Click
> End If
> End Sub
>
> - Jon
> -------
> Jon Peltier, Microsoft Excel MVP
> Peltier Technical Services
> Tutorials and Custom Solutions
> http://PeltierTech.com/
> _______
>
>
> PraxisPete wrote:
> > I am no expert but try this in the code module for the UserForm
> > Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
> > If CloseMode = vbFormControlMenu Then
> > Cancel = True
> > End If
> > End Sub
> >
> > "jlroper" wrote:
> >
> >
> >>Does anyone know how to disable the red X on the top right of a form?
> >>
> >>
> >>
> >>--
> >>jlroper
> >>------------------------------------------------------------------------
> >>jlroper's Profile: http://www.excelforum.com/member.php...o&userid=26709
> >>View this thread: http://www.excelforum.com/showthread...hreadid=401423
> >>
> >>
>
Hi jlroper
Sorry for the delay in my reply - only just spotted your question from yesterday. You will probably find that the code you are copying is breaking at a certain points where Excel is expecting them to continue - probably due to the screen width on the forum you are viewing from. I've put together a small example file, just so you can see how it should look and work.
HTH
DominicB
Last edited by dominicb; 08-19-2008 at 04:26 AM.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks