Excel

Screen Resolution Check / Change

Ease of Use

Intermediate

Version tested with

2002 

Submitted by:

Jacob Hilderbrand

Description:

This code will test the users screen resolution. If that resolution is not what you have specified in the code, then the user gets a warning message, along with the Windows dialog box used to change their resolution. 

Discussion:

It is often desirable to have userw work with your program in a specified screen resolution, but it can be rude to simply change it using code as it could impact other programs with which the user works. With this code, the user is informed that they should use a certain resolution, and then gives the user the opportunity to change it via the Windows dialog box. 

Code:

instructions for use

			

Option Explicit Private Sub Workbook_Open() Call VerifyScreenResolution End Sub Option Explicit Private Declare Function GetSystemMetrics Lib "user32.dll" (ByVal nIndex As Long) As Long Const SM_CXSCREEN = 0 Const SM_CYSCREEN = 1 Sub VerifyScreenResolution(Optional Dummy As Integer) Dim x As Long Dim y As Long Dim MyMessage As String Dim MyResponse As VbMsgBoxResult x = GetSystemMetrics(SM_CXSCREEN) y = GetSystemMetrics(SM_CYSCREEN) If x = 1024 And y = 768 Then Else MyMessage = "Your current screen resolution is " & x & " X " & y & vbCrLf & "This program " & _ "was designed to run with a screen resolution of 1024 X 768 and may not function properly " & _ "with your current settings." & vbCrLf & "Would you like to change your screen resolution?" MyResponse = MsgBox(MyMessage, vbExclamation + vbYesNo, "Screen Resolution") End If If MyResponse = vbYes Then Call Shell("rundll32.exe shell32.dll,Control_RunDLL desk.cpl,,3") End If End Sub

How to use:

  1. Copy from Private Sub Workbook_Open() (above) to the first End Sub.
  2. Open your workbook.
  3. Hit Alt+F11 to open the Visual Basic Editor (VBE).
  4. Double-click the ThisWorkbook on the left, under your file's name.
  5. Paste the code into the code window at right.
  6. Copy from Sub VerifyScreenResolution(Optional Dummy As Integer) to the end of the code (above).
  7. Go back to the VBE.
  8. From the menu, choose Insert-Module.
  9. Paste into the code window at right.
  10. Save the file and close it.
 

Test the code:

  1. Make your screen resolution 1024x768.
  2. Open the workbook (nothing should happen).
  3. Close the workbook.
  4. Change your screen resolution to anything except 1024x768.
  5. Open the workbook. You should get a message warning you about your screen resolution.
  6. Press Yes to change your resolution, press No to keep your resolution as it is.
 

Sample File:

CheckScreenResolution.zip 7.49KB 

Approved by mdmackillop


This entry has been viewed 224 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express