Sunday, 25 November 2012

Changing Screen Resolution using VBA

Although the end user might not thank you for it, it's possible to inform the end user that their screen resolution isn't suitable. Perhaps you have a particularly large Userform that requires a certain screen resolution to fit and anything less wouldn't be acceptable.

Whatever the reason, this code will determine the current Screen resolution and then inform the user that the resolution is wrong and ask them if they would like to change the resolution automatically.

Paste the following code into a Module.

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

No comments:

Post a Comment