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 GetSystemMetricsreturns 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
GetScreenResolutionreturns 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
- GetSyetemMetrics . Note: most API references are written for the C language
- This example was developed in Excel 2016 64 bit.
- Revised: Saturday 8th of October 2016 - 10:49 PM, Pacific Time (PT)