Botón de minimizar y maximizar en Form de vba

En base a una pregunta del blog, encontré el siguiente código (el cual no es mi autoría) para poder poner tanto un botón de minimizar y uno de maximizar en un formulario de vba.

Ver video Haz que tus Formularios parezcan aplicaciones

El código va en el formulario. Lo comparto:

Option Explicit
#If VBA7 And Win64 Then
    Private Declare PtrSafe Function FindWindow Lib "USER32" _
    Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr
#Else
    Private Declare Function FindWindow Lib "USER32" _
    Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
#End If

#If VBA7 And Win64 Then
    Private Declare PtrSafe Function DrawMenuBar Lib "USER32" (ByVal hwnd As Long) As LongPtr
#Else
    Private Declare Function DrawMenuBar Lib "USER32" (ByVal hwnd As Long) As Long
#End If

#If VBA7 And Win64 Then
    #If VBA7 Then
        #If Win64 Then
            Private Declare PtrSafe Function SetWindowLongPtr Lib "USER32" Alias "SetWindowLongPtrA" _
            (ByVal hwnd As LongPtr, ByVal nIndex As Long, ByVal dwNewLong As LongPtr) As LongPtr
        #Else
            Private Declare Function SetWindowLongPtr Lib "USER32" Alias "SetWindowLongA" _
            (ByVal hwnd As LongPtr, ByVal nIndex As Long, ByVal dwNewLong As LongPtr) As LongPtr
        #End If
    #Else
        Private Declare Function SetWindowLongPtr Lib "USER32" Alias "SetWindowLongA" _
        (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    #End If
#Else
    Private Declare Function SetWindowLong Lib "USER32" Alias "SetWindowLongA" _
    (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
#End If

#If VBA7 And Win64 Then
    #If VBA7 Then
        #If Win64 Then
            Private Declare PtrSafe Function GetWindowLongPtr Lib "USER32" _
            Alias "GetWindowLongPtrA" (ByVal hwnd As LongPtr, ByVal nIndex As Long) As LongPtr
        #Else
            Private Declare PtrSafe Function GetWindowLongPtr Lib "USER32" _
            Alias "GetWindowLongA" (ByVal hwnd As LongPtr, ByVal nIndex As Long) As LongPtr
        #End If
    #Else
        Private Declare Function GetWindowLongPtr Lib "USER32" Alias "GetWindowLongA" _
        (ByVal hwnd As Long, ByVal nIndex As Long) As Long
    #End If
#Else
    Private Declare Function GetWindowLong Lib "USER32" Alias "GetWindowLongA" _
    (ByVal hwnd As Long, ByVal nIndex As Long) As Long
#End If


Private Const WS_MINIMIZEBOX As Long = &H20000
Private Const WS_MAXIMIZEBOX As Long = &H10000
Private Const GWL_STYLE As Long = (-16)

'AL INICIAR EL FORMULARIO
Private Sub UserForm_Initialize()
Dim Windows64 As Boolean
'
'Validamos la versión de Office
#If VBA7 And Win64 Then
    Dim lngMyHandle As LongPtr, lngCurrentStyle As LongPtr, lngNewStyle As LongPtr
#Else
    Dim lngMyHandle As Long, lngCurrentStyle As Long, lngNewStyle As Long
#End If
'
If Application.Version < 9 Then
    lngMyHandle = FindWindow("THUNDERXFRAME", Me.Caption)
Else
    lngMyHandle = FindWindow("THUNDERDFRAME", Me.Caption)
End If
'
#If VBA7 And Win64 Then
    lngCurrentStyle = GetWindowLongPtr(lngMyHandle, GWL_STYLE)
    lngNewStyle = lngCurrentStyle Or WS_MINIMIZEBOX Or WS_MAXIMIZEBOX
    SetWindowLongPtr lngMyHandle, GWL_STYLE, lngNewStyle
    '
#Else
    lngCurrentStyle = GetWindowLong(lngMyHandle, GWL_STYLE)
    lngNewStyle = lngCurrentStyle Or WS_MINIMIZEBOX Or WS_MAXIMIZEBOX
    SetWindowLong lngMyHandle, GWL_STYLE, lngNewStyle
#End If
'
#If Win64 Then
    Windows64 = True
#Else
    Windows64 = False
#End If
'
End Sub
:: Descargar el ejemplo

You may also like...

16 Responses

  1. Jose says:

    Sergio! muchas gracias por el aporte!

  2. Papasmarrone says:

    Pues felicita de mi parte al autor y gracias por el aporte.

  3. pedro says:

    Es muy interesante, me gusta lo que publicaste creo que lo recomendare, es lo que andaba buscando

  4. Luis Daniel Rodriguez Acuña says:

    tengo un problemita con el codigo se ejcuta normal , pero al querer usar mi formulario me sale error :'(, ingreso un codigo para buscar y me sale error

  5. niko says:

    hola amigo esto solo funciona en pc de 32 bits pero en 64 bits sale error.

  6. Nicolas Quispe Gonzales says:

    Todo esta muy bien amigo pero cuando minimizo el formulario no me deja abrir otro archivo excel. ¿existe alguna solucion?

  7. john says:

    Botón de minimizar y maximizar en Form de vba

    No se puede descargar el ejemplo, da el siguiente error el link de descarga:

    Runtime Error
    Description: An application error occurred on the server. The current custom error settings for this application prevent the details of the application error from being viewed remotely (for security reasons). It could, however, be viewed by browsers running on the local server machine.

    Details: To enable the details of this specific error message to be viewable on remote machines, please create a tag within a “web.config” configuration file located in the root directory of the current web application. This tag should then have its “mode” attribute set to “Off”.

  8. Raul Pema says:

    Buen dia Sergio, 2 cosas:
    1.- Agrego tu codigo a mi formulario y el mismo se colocan todas las instrucciones en color rojo y no me corre.
    2.- he tratado de descargar el complemento exceleinfo y los link estan caidos.

    tengo excel 16, sistema de 32 bit y win 10, español
    gracias de antemano

  1. July 30, 2013

    […] Aprovechando que vamos a ocultar la ventana de Excel, y para hacer que nuestro formulario parezca más una aplicación, también le añadiremos la opción de minimizar. El código para minimizar y maximizar hace referencia a otro post que publiqué anteriormente, donde podemos descargarlo Botón de minimizar y maximizar en Form de vba. […]

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: