[VBA × Windows API] How to Use the MoveWindow Function

MoveWindow Function

The MoveWindow function moves and resizes a specified window in a single call, changing both its position (X, Y) and size (width / height) at once. For UserForm objects you can control position and size through built-in properties, so this function is rarely needed there — but it is very useful when you need to move or resize any other window from VBA.

A key feature of MoveWindow is that, given a window handle, it applies both the move and the resize in one API call. Typical use cases include pinning a window to a fixed position on launch, arranging several tool windows in a tiled layout, or resetting a cluttered window arrangement at the start of a macro.

It is also commonly combined with ShowWindow (to fix a window’s position right after minimizing or restoring it), or with SetForegroundWindow (to bring a window to the front and then reposition it for better visibility).

 

Usage

Before calling MoveWindow, you must declare it at the top of your module.
Without the declaration the function cannot be used and will raise an error, so don’t forget it.

The declaration differs depending on whether your Office/VBA host is 32-bit or 64-bit.
Place one of the following near the top of your module (just after Option Explicit) to make the function available inside that module.

icon-code 64-bit

Declare PtrSafe Function MoveWindow Lib “user32" _
(ByVal hwnd As LongPtr, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal bRepaint As Long) As Long

icon-code 32-bit

Declare Function MoveWindow Lib “user32" _
(ByVal hwnd As Long, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal bRepaint As Long) As Long

If you are not sure which one to use, paste the following block at the top of your module. It automatically selects the correct declaration at compile time.
In the VBE, the unused branch may be shown in red, but this does not affect execution.

#If VBA7 Then
    Declare PtrSafe Function MoveWindow Lib "user32" (ByVal hwnd As LongPtr, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal bRepaint As Long) As Long
#Else
    Declare Function MoveWindow Lib "user32" (ByVal hwnd As Long, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal bRepaint As Long) As Long
#End If

You can also prefix the declaration with Private or Public to control its scope.

Private Declare PtrSafe Function … : callable only within the current module
Public Declare PtrSafe Function … : callable from outside the module

 
 

Syntax

The syntax of the MoveWindow function is as follows.

icon-code MoveWindow function

lRet = MoveWindow(hwnd, X, Y, nWidth, nHeight, bRepaint)

Arguments

hwnd (64-bit: LongPtr / 32-bit: Long)

Handle to the window you want to move or resize.
You typically obtain this handle with FindWindow or a similar API.

X (Long)

New X coordinate of the window’s top-left corner, in pixels.

Y (Long)

New Y coordinate of the window’s top-left corner, in pixels.

nWidth (Long)

New window width, in pixels.

nHeight (Long)

New window height, in pixels.

bRepaint (Long)

Whether to repaint the window. Normally pass 1 (True).
(Passing 0 suppresses the repaint.)

Return value

lRet  (Long)

Non-zero if the function succeeds, 0 if it fails.

 

Sample Code

The sample below uses FindWindow to get the handle of Notepad, then calls MoveWindow to change both its position and size. Launch Notepad before running the macro.

Option Explicit
Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr
Private Declare PtrSafe Function MoveWindow Lib "user32" (ByVal hWnd As LongPtr, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal bRepaint As Long) As Long

Sub main()

    Dim hWnd As LongPtr
    Dim lRet As Long

    ' Get the Notepad window handle
    hWnd = FindWindow("Notepad", vbNullString)

    ' Move to (50, 50), resize to 420 x 260
    lRet = MoveWindow(hWnd, 50, 50, 420, 260, 1)
    If lRet = 0 Then
        Call MsgBox("MoveWindow failed.", vbCritical)
    End If

End Sub

 

icon-share-square VBA × Windows API Index

For other Windows API functions, please also refer to the index page below.

icon-share-square Reference

Microsoft official: MoveWindow function (winuser.h) — Win32 apps

Excel,VBA,Windows API