Check if system Is Locked or Unlocked using VBA

vds1

Well-known Member
Joined
Oct 5, 2011
Messages
1,194
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!
 

Some videos you may like

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.

wigi

Well-known Member
Joined
Jul 10, 2006
Messages
7,958
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
  3. Web
Thank you for sharing !
 

Milu

New Member
Joined
Feb 11, 2012
Messages
16
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.
 

wigi

Well-known Member
Joined
Jul 10, 2006
Messages
7,958
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
  3. Web
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.
 

pawanhsharma

Board Regular
Joined
Feb 12, 2013
Messages
63
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
 

Milu

New Member
Joined
Feb 11, 2012
Messages
16

ADVERTISEMENT

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.
 

pratyaysarker

New Member
Joined
Sep 29, 2014
Messages
5
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.
 

pawanhsharma

Board Regular
Joined
Feb 12, 2013
Messages
63
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.
 

anurupgh10

New Member
Joined
Sep 4, 2015
Messages
1
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
 

Watch MrExcel Video

Forum statistics

Threads
1,122,224
Messages
5,594,914
Members
413,952
Latest member
JGer

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
Top