• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

How to auto resize userform in excel?

Abhinav

New Member
Hi,

I am trying to write below code in order to resize the userform i have created. However, I am getting the below error message. Can anyone help to debug this.

My system and MS office versions are: 64 bit

The error message is:
"Run-time error '453':
Can't find DLL entry point Get(Set)WindowLongPtrW in user32.dll


Below is the code I have written:
-----------------------------------------
Code:
Public Const SW_HIDE            As Long = 0
Public Const SW_NORMAL          As Long = 1
Public Const SW_SHOWMINIMIZED   As Long = 2
Public Const SW_MAXIMIZED       As Long = 3

Public Const WS_MAXIMIZEBOX     As Long = &H10000
Public Const WS_MINIMIZEBOX     As Long = &H20000
Public Const WS_THICKFRAME      As Long = &H40000
Public Const WS_MAXIMIZE        As Long = &H1000000
Public Const GWL_STYLE          As Long = -16


#If Win64 Then


    Public Declare PtrSafe Function GetWindowLong _
        Lib "user32.dll" Alias "Get(Set)WindowLongPtrW" _
            (ByVal hwnd As LongPtr, _
             ByVal nIndex As LongLong) _
        As LongPtr


    Public Declare PtrSafe Function SetWindowLong _
        Lib "user32.dll" Alias "SetWindowLongPtr" _
            (ByVal hwnd As LongPtr, _
             ByVal nIndex As Long, _
             ByVal dwNewLong As LongPtr) _
        As LongPtr


    Public Declare PtrSafe Function ShowWindow _
        Lib "user32.dll" _
            (ByVal hwnd As LongPtr, _
             ByVal nCmdShow As Long) _
        As Long
       
    Public Declare PtrSafe Function GetForegroundWindow Lib "user32.dll" () As LongPtr
 

    Public Sub MakeFormResizable()

        Dim lStyle  As LongPtr
        Dim hwnd    As LongPtr
        Dim RetVal  As LongPtr
 
            hwnd = GetForegroundWindow
 
            'Get the basic window style
            lStyle = GetWindowLong(hwnd, GWL_STYLE) Or WS_THICKFRAME Or WS_MINIMIZEBOX Or WS_MAXIMIZEBOX


            'Set the basic window styles
            RetVal = SetWindowLong(hwnd, GWL_STYLE, lStyle)

    End Sub

#Else
              
    Public Declare Function GetWindowLong _
        Lib "user32.dll" Alias "GetWindowLongA" _
            (ByVal hwnd As Long, _
             ByVal nIndex As Long) _
        As Long
   
    Public Declare Function SetWindowLong _
        Lib "user32.dll" Alias "SetWindowLongA" _
            (ByVal hwnd As Long, _
             ByVal nIndex As Long, _
             ByVal dwNewLong As Long) _
        As Long


    Public Declare Function ShowWindow _
    Lib "user32.dll" _
        (ByVal hwnd As Long, _
         ByVal nCmdShow As Long) _
    As Long


    Public Declare Function GetForegroundWindow Lib "user32.dll" () As Long


    Public Sub MakeFormResizable()

        Dim lStyle  As Long
        Dim hwnd    As Long
        Dim RetVal  As Long
 
            hwnd = GetForegroundWindow
 
            'Get the basic window style
            lStyle = GetWindowLong(hwnd, GWL_STYLE) Or WS_THICKFRAME Or WS_MINIMIZEBOX Or WS_MAXIMIZEBOX


            'Set the basic window styles
            RetVal = SetWindowLong(hwnd, GWL_STYLE, lStyle)

    End Sub

#End If
 
Last edited by a moderator:

Chihiro

Excel Ninja
As the error message suggests, there is no such API end/entry point as "Get(Set)WindowLongPtrW"

As well, your declaration for the function is off.

For Win64 you'd declare it as...
Code:
        Private Declare PtrSafe Function GetWindowLongPtr Lib "USER32" Alias "GetWindowLongPtrA" (ByVal hWnd As LongPtr, ByVal nIndex As Long) As LongPtr
There are other issues with your code at quick glance. I'd recommend reading below article.
https://docs.microsoft.com/en-us/office/client-developer/shared/compatibility-between-the-32-bit-and-64-bit-versions-of-office
 
Top