Home Excel Powerpoint Word Outlook Error Codes

VBA Useful Codes

Welcome to the World of VBA
Home > VBAUsefulCodes

Find System Lock Status Locked Or Unlocked using Excel VBA

This sample code provides the functionality to check whether the system is locked or not.

' Paste this code in Module
' for Demonstration purpose a timer has been setup to run the macro during locked mode

Private Declare Function SwitchDesktop Lib "user32" (ByVal hDesktop As Long) As Long
Private Declare Function OpenDesktop Lib "user32" Alias "OpenDesktopA" (ByVal lpszDesktop As String, ByVal dwFlags As Long, ByVal fInherit As Long, ByVal dwDesiredAccess As Long) As Long
Private Declare Function CloseDesktop Lib "user32" (ByVal hDesktop As Long) As Long
Private Const DESKTOP_SWITCHDESKTOP As Long = &H100

Public Sub setTimer()
Application.OnTime TimeValue("17:55:30"), "status"
End Sub

Public Function status()
Dim p_lngHwnd As Long
Dim p_lngRtn As Long
Dim p_lngErr As Long

p_lngHwnd = OpenDesktop(lpszDesktop:="Default", dwFlags:=0, fInherit:=False, dwDesiredAccess:=DESKTOP_SWITCHDESKTOP)

If p_lngHwnd = 0 Then
status = "Error"
p_lngRtn = SwitchDesktop(hDesktop:=p_lngHwnd)
p_lngErr = Err.LastDllError

If p_lngRtn = 0 Then
If p_lngErr = 0 Then
status = "Locked"
status = "Error"
End If
status = "Unlocked"
End If

p_lngHwnd = CloseDesktop(p_lngHwnd)
End If
MsgBox status
End Function


Enter your e-mail below and get notified on the latest blog posts.


VBA Outlook KPI Excel Alt+F11 Dashboards Macros Recording Graphs Automation Developer WaterFall Powerpoint Charts Pivot Tables Forecast Charts

Follow Us