VBA – Turn Off The Monitor

Once again, helping someone out in a forum with a question, I put together the following to turnoff the PC monitors. It turn all the monitors off. As per the usual, I’m posting it here should it be able to help someone else out.

'32-bit declaration
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, _
                                                                        ByVal wMsg As Long, _
                                                                        ByVal wParam As Long, _
                                                                        ByVal lParam As Any) As Long
'64-bit declaration
'Private Declare PtrSafe Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As LongPtr, _
'                                                                                ByVal wMsg As Long, _
'                                                                                ByVal wParam As LongPtr, _
'                                                                                lParam As Any) As LongPtr

'---------------------------------------------------------------------------------------
' Procedure : MonitorPower
' Author    : Daniel Pineault, CARDA Consultants Inc.
' Website   : http://www.cardaconsultants.com
' Purpose   : Turn On/Off the monitor
' Copyright : The following is release as Attribution-ShareAlike 4.0 International
'             (CC BY-SA 4.0) - https://creativecommons.org/licenses/by-sa/4.0/
' Req'd Refs: None required
' Req'd     : SendMessage API Declaration
'
' Input Variables:
' ~~~~~~~~~~~~~~~~
' bMontiorOn: True -> Turn monitor on
'             False -> Turn monitor off
'
' Usage:
' ~~~~~~
' Call MonitorPower
'
' Revision History:
' Rev       Date(yyyy/mm/dd)        Description
' **************************************************************************************
' 1         2018-12-14              Initial Release (Forum help)
'---------------------------------------------------------------------------------------
Public Function MonitorPower(Optional bMontiorOn As Boolean = False)
'REF: https://docs.microsoft.com/en-us/windows/desktop/menurc/wm-syscommand
    Const WM_SYSCOMMAND = &H112
    Const SC_MONITORPOWER = &HF170&
    Const MONITOR_ON = -1&
    Const MONITOR_OFF = 2&

    On Error GoTo Error_Handler

    If bMontiorOn = False Then
        SendMessage Application.hWndAccessApp, WM_SYSCOMMAND, SC_MONITORPOWER, MONITOR_OFF
    Else
        SendMessage Application.hWndAccessApp, WM_SYSCOMMAND, SC_MONITORPOWER, MONITOR_ON
    End If

Error_Handler_Exit:
    On Error Resume Next
    Exit Function

Error_Handler:
    MsgBox "The following error has occurred" & vbCrLf & vbCrLf & _
           "Error Number: " & Err.Number & vbCrLf & _
           "Error Source: MonitorPower" & vbCrLf & _
           "Error Description: " & Err.Description & _
           Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
           , vbOKOnly + vbCritical, "An Error has Occurred!"
    Resume Error_Handler_Exit
End Function

Also note, that the screen will turn back on when the user moves the mouse or touches the keyboard making MonitorPower(True) somewhat unnecessary, but it can still be useful if used through code. It does not activate the screensaver, nor does it lock the PC. The only thing it does is power off the screen, so the screen goes black until the PC receives some form of input from the user, just like what happens through the power saving settings of a PC.