Results 1 to 14 of 14
  1. #1
    Silver Lounger
    Join Date
    Mar 2001
    Location
    Springfield, Ohio, USA
    Posts
    2,136
    Thanks
    0
    Thanked 1 Time in 1 Post

    Hide Close Button (Any)

    How can I hide the close button of a UserForm? I assume that I will have to use API calls, but I cannot find any sample code. TIA --Sam
    BTW, I know how to disable it with the QueryClose event, but I would also like to make it disappear.
    <font face="Comic Sans MS">Sam Barrett, CACI </font face=comic>
    <small>And the things that you have heard... commit these to faithful men who will be able to teach others also. 2 Timothy 2:2</small>

  2. #2
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Hide Close Button (Any)

    You could set the BorderStyle property to "None" ?

  3. #3
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Hide Close Button (Any)

    See p2p.wrox.com Forums - Get rid of the close button on a form. The code in the last reply does what you want.

  4. #4
    Silver Lounger
    Join Date
    Mar 2001
    Location
    Springfield, Ohio, USA
    Posts
    2,136
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Hide Close Button (Any)

    I had it all except the WS_SYSMENU constant. Thanks! --Sam
    <font face="Comic Sans MS">Sam Barrett, CACI </font face=comic>
    <small>And the things that you have heard... commit these to faithful men who will be able to teach others also. 2 Timothy 2:2</small>

  5. #5
    Silver Lounger
    Join Date
    Mar 2001
    Location
    Springfield, Ohio, USA
    Posts
    2,136
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Hide Close Button (Any)

    Four or five comments to finish the thread:

    1) Dave said to "set the BorderStyle property to 'None'." This only works in VB and it removes the title bar. I'm working in VBA.
    2) I couldn't get the Wrox code to work, but thanks to a post from Alan Miller, I found the code that I wanted at http://word.mvps.org/FAQs/Userforms/DisableClose.htm
    3) I found some other threads that pointed out that <ALT><F4> would also close the form, so I added a QueryClose event.
    The final code for a UserForm with a single command button to close is below. It looks very nice with the X Button grayed out. Thanks everyone for your help! --Sam
    <pre>Option Explicit
    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

    Private OKtoClose As Boolean

    Private Sub UserForm_Initialize()

    Dim hMenu As Long
    Dim menuItemCount As Long


    'Obtain the window handle to the userform
    hWnd = FindWindow(vbNullString, Me.Caption)

    '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

    Private Sub CommandButton1_Click()
    OKtoClose = True
    Unload Me
    End Sub

    Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    Cancel = Not OKtoClose
    End Sub
    </pre>

    <font face="Comic Sans MS">Sam Barrett, CACI </font face=comic>
    <small>And the things that you have heard... commit these to faithful men who will be able to teach others also. 2 Timothy 2:2</small>

  6. #6
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: Hide Close Button (Any)

    Hans,
    That doesn't work on my Excel 2002 SP3 - the X is still there (though the QueryClose obviously stops it from working.)
    Regards,
    Rory

    Microsoft MVP - Excel

  7. #7
    Silver Lounger
    Join Date
    Mar 2001
    Location
    Springfield, Ohio, USA
    Posts
    2,136
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Hide Close Button (Any)

    Works for me on XL 02 sp3. Thanks! I guess you have to decide if you wany grayed-out (like the MVP version) or gone (like the Wrox version).
    <font face="Comic Sans MS">Sam Barrett, CACI </font face=comic>
    <small>And the things that you have heard... commit these to faithful men who will be able to teach others also. 2 Timothy 2:2</small>

  8. #8
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Hide Close Button (Any)

    That's strange - those API functions are supposed to work on any version of Windows and Office. <img src=/S/scratch.gif border=0 alt=scratch width=25 height=29>

  9. #9
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: Hide Close Button (Any)

    <img src=/S/doh.gif border=0 alt=doh width=15 height=15>I've figured out why it doesn't work - I have a skin applied to my system (using WindowBlinds) which seems to override the effect. If I unload the skin, all works as expected!
    Regards,
    Rory

    Microsoft MVP - Excel

  10. #10
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Hide Close Button (Any)

    <img src=/S/blackteeth.gif border=0 alt=blackteeth width=20 height=20>

    Thanks for posting this - might well be useful to others.

  11. #11
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Hide Close Button (Any)

    > might well be useful to others
    Useful?
    I dunno about that.
    It's made me look good in the eyes of one client already ........ (grin)

  12. #12
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Hide Close Button (Any)

    I found (Word2000) that the supplied code for UserForm_Initialize works not well unless it is the final code in the procedure.
    In my trivial example below, the line "Me.Caption" leaves a close button on the form - presumably because in re-writing the form title bar, WordVBA dumps the close button back in there.
    Disabling my "Me.Caption" line leaves the close button hidden.
    Moving my "Me.Caption" line to the head of the procedure leaves the close button hidden.

    <pre>Private Sub UserForm_Initialize()
    Dim hWnd As Long, lStyle As Long

    'Which type of userform
    If Val(Application.Version) >= 9 Then
    hWnd = FindWindow("ThunderDFrame", Me.Caption)
    Else
    hWnd = FindWindow("ThunderXFrame", Me.Caption)
    End If

    'Get the current window style and turn off the Close button
    lStyle = GetWindowLong(hWnd, GWL_STYLE)
    SetWindowLong hWnd, GWL_STYLE, (lStyle And Not WS_SYSMENU)
    <font color=red>Me.Caption = "won't work"</font color=red>
    End Sub</pre>


  13. #13
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Hide Close Button (Any)

    Indeed, changing the caption forces VBA to redraw the title bar. And since removing the close button is not officially supported, the close button is displayed again. You've found the workaround yourself.

  14. #14
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Hide Close Button (Any)

    Edited by HansV to re-attach file that was lost in the Big Crash of 2007

    The attached demo (zipped) uses the code from the Wrox forum. It works for me in Excel 2002 SP-3. I haven't tested it on other versions.
    Attached Files Attached Files

Posting Permissions

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