[VBA] How to Get Screen Resolution (Width and Height) (GetSystemMetrics API)

目次

Introduction

When creating UserForms in VBA, optimizing the display position and size based on the user’s screen size is often necessary. For example, centering a form according to the screen resolution or adjusting a window so it does not extend beyond the screen boundaries.

To retrieve such PC environment information, Windows API functions are used. The screen resolution (width and height) can be easily obtained using the GetSystemMetrics API function.

This article explains how to retrieve the screen resolution of the primary monitor in pixels using the GetSystemMetrics API.

VBA Sample Code to Get Screen Resolution

The Declare statements and Const definitions must be written at the very top of the module (before any procedures like Sub).

Complete Code

'--- Declare API functions and constants at the top of the module ---

' Compatible with both 64-bit and 32-bit Office
#If VBA7 Then
    ' API function to get various system metrics
    Private Declare PtrSafe Function GetSystemMetrics Lib "user32" (ByVal nIndex As Long) As Long
#Else
    Private Declare Function GetSystemMetrics Lib "user32" (ByVal nIndex As Long) As Long
#End If

' Constants used in GetSystemMetrics
Private Const SM_CXSCREEN As Long = 0 ' Constant to get screen width
Private Const SM_CYSCREEN As Long = 1 ' Constant to get screen height


' Sub to retrieve and display the primary monitor's resolution
Sub GetScreenResolution()
    Dim screenWidth As Long
    Dim screenHeight As Long
    
    '--- Call the API function to get width and height ---
    screenWidth = GetSystemMetrics(SM_CXSCREEN)
    screenHeight = GetSystemMetrics(SM_CYSCREEN)
    
    '--- Display the results in a message box ---
    MsgBox "Screen Resolution:" & vbCrLf & _
           "Width: " & screenWidth & " pixels" & vbCrLf & _
           "Height: " & screenHeight & " pixels", _
           vbInformation, "Resolution Info"
End Sub

Explanation of the Code

Declare PtrSafe Function GetSystemMetrics ...

This is the declaration to make the Windows API function GetSystemMetrics available in VBA. By passing specific constants as arguments to this function, various system information (number of mouse buttons, width of scroll bars, etc.) can be retrieved.

Const SM_CXSCREEN and SM_CYSCREEN

These are the constants that tell the GetSystemMetrics function “what information is requested.”

ConstantValueDescription
SM_CXSCREEN0Retrieves the Width (X-axis) of the primary monitor in pixels.
SM_CYSCREEN1Retrieves the Height (Y-axis) of the primary monitor in pixels.

screenWidth = GetSystemMetrics(SM_CXSCREEN)

By calling the GetSystemMetrics function and passing SM_CXSCREEN as the argument, the screen width is retrieved and stored in the variable screenWidth. The same logic applies to the height using SM_CYSCREEN.

Summary

This article explained how to retrieve screen resolution using the Windows API GetSystemMetrics.

  • GetSystemMetrics API can retrieve various system metrics.
  • Pass SM_CXSCREEN to get the width and SM_CYSCREEN to get the height in pixels.

Using this technique enables layout control for UserForms that considers the user’s screen size, allowing for a more user-friendly interface design.

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次