How to determine Monitor resolution via VBA code

How can I use VBA code to determine the resoltuion of the monitor (eg 1280 x 1024)?
LVL 1
MilewskpAsked:
Who is Participating?
 
doctorzoomConnect With a Mentor Commented:
Try this:

Private Declare Function GetSystemMetrics Lib "user32.dll" (ByVal nIndex As Long) As Long
Const SM_CXSCREEN = 0
Const SM_CYSCREEN = 1
 
Sub CheckScreenResolution()
     
    Dim x  As Long
    Dim y  As Long
         
    x = GetSystemMetrics(SM_CXSCREEN)
    y = GetSystemMetrics(SM_CYSCREEN)

'now you have screen resolution in x and y

end sub
0
 
nffvrxqgrcfqvvcCommented:
You can use GetMonitorInfo API

Option Explicit

Private Const MONITOR_DEFAULTTOPRIMARY As Long = &H1&

Private Type RECT
rLeft As Long
rTop As Long
rRight As Long
rBottom As Long
End Type

Private Type MONITORINFO
cbSize As Long
rcMonitor As RECT
rcWork As RECT
dwFlags As Long
End Type

Private Declare Function GetMonitorInfoW Lib "User32.dll" (ByVal hMonitor As Long, ByRef lpmi As MONITORINFO) As Long

Private Sub Form_Load()
  Dim mi As MONITORINFO
  mi.cbSize = Len(mi)
  GetMonitorInfoW MONITOR_DEFAULTTOPRIMARY, mi
  Debug.Print mi.rcMonitor.rRight; mi.rcMonitor.rBottom
End Sub

Open in new window

0
 
MilewskpAuthor Commented:
For computers with multiple monitors, which one are the results for? is there a way to get the res of all monitors?
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
nffvrxqgrcfqvvcCommented:
They are for the primary display. I don't have that type of rig but you could try the following code example.
'// Usage

EnumMonitors

Open in new window

'//
'// Module1.bas
'//

Option Explicit

Private Type RECT
rLeft As Long
rTop As Long
rRight As Long
rBottom As Long
End Type

Private Declare Function EnumDisplayMonitors Lib "User32.dll" (ByVal hdc As Long, ByVal lprcClip As Long, ByVal lpfnEnum As Long, ByVal dwData As Long) As Long

Public Function EnumMonitors()
  '// Enumerates all display monitors.
  EnumDisplayMonitors 0, 0, AddressOf MonitorEnumProc, 0
End Function

Public Function MonitorEnumProc(ByVal hMonitor As Long, ByVal hdcMonitor As Long, ByRef lprcMonitor As RECT, ByVal dwData As Long) As Long
  Debug.Print lprcMonitor.rRight; lprcMonitor.rBottom
  MonitorEnumProc = 1
End Function

Open in new window

0
 
MilewskpAuthor Commented:
Hi ,
It looks like GetSystemMetrics() can't give you the resolution of each monitor, but will give the Width and height, in pixels, of the virtual screen (the bounding rectangle of all display monitors). Supported in Windows CE .NET 4.0 and later.
- http://msdn.microsoft.com/en-us/library/aa929225.aspx 
0
 
MilewskpAuthor Commented:
Thanks to both of you for your input.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.