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

16 thoughts on “Botón de minimizar y maximizar en Form de vba”

  1. Pingback: EXCELeINFO – Excel vba e información - Hacer que un Formulario de Excel parezca una aplicación con botones de minimizar y maximizar

  2. Luis Daniel Rodriguez Acuña

    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

    1. Hola, a mí me funciona en algunos formularios, en otros no, es decir en algunos me deja minimizar y ver otros archivos de excel pero en otros formularios no, ¿Es por el mismo error de 32 y 64 bit?

  3. Nicolas Quispe Gonzales

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

  4. 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”.

  5. 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

Leave a Comment

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

Scroll to Top