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.
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
- GetSyetemMetrics . Note: most API references are written for the C language
- This example was developed in Excel 2016 64 bit.
- Revised: Friday 24th of February 2023 - 02:37 PM, Pacific Time (PT)