GetScreenResolution macro

xlf | GetScreenResolution macro


GetScreenResolution - about the macro


Description: returns the screen dimensions of the primary monitor (windows display settings).

The dimension information is displayed in a message box. Example output is shown in figure 1.


screen resolution
Fig 1. Screen dimensions message box - displaying the output from the GetScreenResolution macro.


GetSelectDim - the VBA code


Place code 1 in the module declaration section. This function declaration tell the VBA interpreter that there is a Windows API function named GetSyetemMetrics located in the Windows user32.dll file. The PtrSafe keyword in code 1 line 1, allows the function to run in a Windows 64 bit and Windows 32 environment. PtrSafe was introduced in VBA 7.



Code 1: Declaration Function GetSystemMetrics returns the dimension of the primary monitor
Private Declare PtrSafe Function GetSystemMetrics Lib "user32.dll" _
    (ByVal nIndex As Long) As Long
Const SM_CXSCREEN = 0
Const SM_CYSCREEN = 1

Run code 2 to display the message box shown in figure 1.



Code 2: Macro GetScreenResolution returns the dimension of the primary monitor
Sub GetScreenResolution()
Dim w As Long, h As Long

    ' width and height of primary display monitor in pixels
    w = GetSystemMetrics(SM_CXSCREEN)
    h = GetSystemMetrics(SM_CYSCREEN)
    
    MsgBox w & " x " & h & " pixels", vbInformation, "xlf :: Screen Resolution                                "
    
End Sub											

References