Is there a way to remove the close forms button in VBA / Excel?

2

I want to get the close button of some forms in VBA / Excel.

For example, I'm customizing the presentation of some messages to differentiate from VBA options that are unattractive.

In addition to using different images, colors and fonts, it would be interesting if the form that will present the message does not have the close button (the "X" in the upper right corner), forcing the user to choose "Yes" or "No", for example, and add the "Cancel" button when this option exists.

Can you do this?

I found some solutions, but they are for older versions of VBA and for 32-bit, I could not adapt them to work on 64-bit.

    
asked by anonymous 23.08.2016 / 01:22

1 answer

3

The solution I found is on the Tomas Vasquez Sites site.

DISABLING THE BUTTON CLOSING A USERFORM IN VBA

This is the code:

Option Explicit

' Fonte: Tomas Vasquez Sites
'
' http://www.tomasvasquez.com.br/blog/microsoft-office/vba/desabilitando-o-botao-fechar-de-um-userform-no-vba

'==========================================================================
' Retira o botão "X" (fechar) do Formulário, permanecem a barra e o caption
'==========================================================================

Private Declare Function FindWindowA Lib "user32" _
         (ByVal lpClassName As String, _
          ByVal lpWindowName As String) _
             As Long

Private Declare Function GetWindowLongA Lib "user32" _
         (ByVal hwnd As Long, _
          ByVal nIndex As Long) _
             As Long

Private Declare Function SetWindowLongA Lib "user32" _
         (ByVal hwnd As Long, _
          ByVal nIndex As Long, _
          ByVal dwNewLong As Long) _
             As Long

Private Sub UserForm_Initialize()

Dim hwnd As Long

hwnd = FindWindowA(vbNullString, Me.Caption)

SetWindowLongA _
 hwnd, -16, _
 GetWindowLongA(hwnd, -16) And &HFFF7FFFF

End Sub


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

Dim hwnd As Long

hwnd = FindWindowA(vbNullString, Me.Caption)

SetWindowLongA _
 hwnd, -16, _
 GetWindowLongA(hwnd, -16) Or &H80000

End Sub


Private Sub ButtonSair_Click()

Unload Me

End Sub

The form needs a close button in this case.

    
23.08.2016 / 01:26