Userform close button
Upcoming Power Excel Seminars
Thanks Thanks:  0
Likes Likes:  0
Results 1 to 7 of 7

Thread: Userform close button

  1. #1
    New Member
    Join Date
    Apr 2002
    Location
    East Texas
    Posts
    38
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    Is There any way to disable the Close X button on a userform.

    Thanks in advance

  2. #2
    Board Regular
    Join Date
    Mar 2002
    Location
    =ActiveCell.Address
    Posts
    478
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    I asked the very same question myself a while ago. Jackeb and Sbirro Cornuto kindly obliged...

    http://www.mrexcel.com/board/viewtop...c=3439&forum=2


  3. #3
    MrExcel MVP Mark O'Brien's Avatar
    Join Date
    Feb 2002
    Location
    Columbus, OH, USA
    Posts
    3,530
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    This code will disable the close button and will actually "grey" it out to show that's disabled:


    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Private Declare Function GetSystemMenu Lib "user32" (ByVal hWnd As Long, ByVal bRevert As Long) As Long
    Private Declare Function DeleteMenu Lib "user32" (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long) As Long

    Private Const SC_CLOSE As Long = &HF060

    Private Sub UserForm_Initialize()
    Dim hWndForm As Long
    Dim hMenu As Long

    If Val(Application.Version) < 9 Then
    hWndForm = FindWindow("ThunderXFrame", Me.Caption) 'XL97
    Else
    hWndForm = FindWindow("ThunderDFrame", Me.Caption) 'XL2000
    End If

    'Grey out Close button
    hMenu = GetSystemMenu(hWndForm, 0)
    DeleteMenu hMenu, SC_CLOSE, 0&
    End Sub


    HTH

  4. #4
    New Member
    Join Date
    Apr 2002
    Location
    East Texas
    Posts
    38
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    Thanks for the quick reponse. This has got to be the best board around. Hope one day to be able to help.

  5. #5
    New Member
    Join Date
    Jul 2002
    Posts
    41
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    Mark, WHERE do I have to place your code. I don't understand the meaning of UserForm. Do I have to use a button? I want also to grey the x-button but only when a condition is true. How do I have to do this?

  6. #6
    MrExcel MVP Mark O'Brien's Avatar
    Join Date
    Feb 2002
    Location
    Columbus, OH, USA
    Posts
    3,530
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    Gandalf, welcome to the board.

    If you don't know what a Userform is, then I'm assuming that you want to grey out one of the "X" buttons in Excel itself? Either for the Workbook, or for the Excel application?

    Is this correct?

  7. #7
    MrExcel MVP Mark O'Brien's Avatar
    Join Date
    Feb 2002
    Location
    Columbus, OH, USA
    Posts
    3,530
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    To disable the close button in Excel you can use this code plus the "test" procedure to check it:


    Option Explicit

    Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Public Declare Function GetSystemMenu Lib "user32" (ByVal hWnd As Long, ByVal bRevert As Long) As Long
    Public Declare Function DeleteMenu Lib "user32" (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long) As Long

    Private Const SC_CLOSE As Long = &HF060

    Public Sub XBoxEnabled(ByVal bEnabled As Boolean)
    Dim hWndForm As Long
    Dim hMenu As Long

    hWndForm = FindWindow("XLMAIN", Application.Caption)

    hMenu = GetSystemMenu(hWndForm, bEnabled)
    DeleteMenu hMenu, SC_CLOSE, 0&
    End Sub

    Public Sub test()

    XBoxEnabled True

    End Sub


    As soon as someone tells me what the Classname is for a worksheet, we can disable that little bugger as well.

    HTH

User Tag List

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

 

DMCA.com