Check if system Is Locked or Unlocked using VBA

vds1

Well-known Member
Joined
Oct 5, 2011
Messages
1,200
This code helps to find out if computer has been locked or unlocked.

I have used it in form timer event. Set the timer interval to 1000, run the form and then Look at the Immediate window

Code:
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
 
 
Function Check_If_Locked() As String
    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
        system = "Error"
    Else
        p_lngRtn = SwitchDesktop(hDesktop:=p_lngHwnd)
        p_lngErr = Err.LastDllError
         
        If p_lngRtn = 0 Then
            If p_lngErr = 0 Then
                system = "Locked"
            Else
                system = "Error"
            End If
        Else
            system = "Unlocked"
        End If
        p_lngHwnd = CloseDesktop(p_lngHwnd)
    End If
    Check_If_Locked = system
End Function

Private Sub Form_Timer()
Debug.Print Check_If_Locked
End Sub

Hope this helps someone out there!
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Hi Team,

This is realy very good macro. very useful for me.
Now can you please help me about Below issue.
I want to lock pc using send key. When i press windows + l my macro will work then pc will get lock. This is very very imp for me. Can you please help me to do this.
 
Upvote 0
Windows + l (lowercase L) is enough to lock the PC.
I do not understand why you would use an Access macro to use SendKeys (rarely a good idea) to eventually lock the PC !
I must be missing something here. Or you.
 
Upvote 0
I am wondering can we track the total time system was locked so that we can track actual creak time of a user instead counting system idle time which can be false if the user doesent have any work why the hell he will use keyboard and mouse isn't it
 
Upvote 0
Hello Team,

This is very usefull macro. But can we do this in .vbs file?
It will helpfull for me if we can do this in .vbs file.
Please Please help to do this.
 
Upvote 0
Dear Excel experts

I am a novice and recently started trying my hand with vba codes. I am currently in the process of making a excel based macro which among other things need to capture the system Lock and Unlock time sequentially for an user who has the related excel file open. the captured time needs to be saved on a sheet in the active workbook in the below format:

eg.
System Lock Time | System Unlock Time | Time duration for system Lock

10:20:33 PM | 10:35:22 PM | 00:14:49

I checked on the code that is mentioned here. It gives you the Lock and Unlock time against Timer function (say for every 2 sec.) if I pump the output to a msgbox. However, I am unable to capture the related time in the above mentioned format in an excel file. Desperately need your help. Once again, would be endebted for any assistance. Thanks in advance.
 
Upvote 0
This code helps to find out if computer has been locked or unlocked.

I have used it in form timer event. Set the timer interval to 1000, run the form and then Look at the Immediate window

Code:
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
 
 
Function Check_If_Locked() As String
    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
        system = "Error"
    Else
        p_lngRtn = SwitchDesktop(hDesktop:=p_lngHwnd)
        p_lngErr = Err.LastDllError
         
        If p_lngRtn = 0 Then
            If p_lngErr = 0 Then
                system = "Locked"
            Else
                system = "Error"
            End If
        Else
            system = "Unlocked"
        End If
        p_lngHwnd = CloseDesktop(p_lngHwnd)
    End If
    Check_If_Locked = system
End Function

Private Sub Form_Timer()
Debug.Print Check_If_Locked
End Sub

Hope this helps someone out there!

Wonderful Code!!! Can this be used to check if a remote system is locked. there must be some way.
 
Upvote 0
Can anyone explain the program...
and what is Form Timer....

I want to call an commandButton1_click() whenever Windows get locked.

So where should i keep thses code in Excel VBA.

Thanks In Advance
 
Upvote 0

Forum statistics

Threads
1,223,570
Messages
6,173,126
Members
452,502
Latest member
perrygreen98

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top