Excel

Lock Excel if No Activity Then Log-off if Wrong PIN Entered

Ease of Use

Intermediate

Version tested with

2000, 2002, 2003 

Submitted by:

johnske

Description:

On opening the workbook a timer is started, if the user has any 2 minute period of inactivity after that; an input box will request the user to input a PIN to continue. If they enter the wrong PIN three times, all open workbooks are saved, closed, and the current Windows session terminated. 

Discussion:

In a shared workplace environment there may be occasions where a primary user may have a workbook open and be called away from their workstation for an unexpectedly large period of time. This procedure goes a long way to ensure that an unauthorized person cannot access the primary users file while they are absent. By using a custom right-click menu item Excel can be immediately locked by the primary user as they leave their workstation, but if they forget to lock it, a timer will call the procedure after a set period of time and lock Excel anyway, requiring the use of a PIN to access any Excel file (note that other unprotected applications can still be accessed during this period)... If there are then three failed attempts to enter the PIN to access Excel, this procedure will close ALL applications and exit Windows, not just Excel. ***NOTE: As this closes the current Windows log-in session, it's possible that the closure could cause loss of data or data corruption in some open applications, this is something to be weighed against the risk and consequences of unauthorised access to your file(s). This decision is yours alone, hence - YOU USE AT YOUR OWN RISK! REFER TO OUR TERMS OF USE OR A MODERATOR FOR ANY QUESTIONS!.*** (For other options such as rebooting, shutting down the PC, or a forced (emergency, nothing saved) Log-off see this article > http://www.vbaexpress.com/kb/getarticle.php?kb_id=515) Caveat: If there is another application open at the same time, it's possible to cancel the log-off part of the procedure by clicking 'Cancel' when asked if you want to save this other application. To circumvent this, you would need to use "Force" (the other application is then not saved). 

Code:

instructions for use

			

'********************************************* '<< CODE FOR THISWORKBOOK MODULE >> Option Explicit Private Sub Workbook_Open() '//remove any previous right-click control Run "RemoveFromMenu" '//now add new right-click control With Application.CommandBars("Cell") .Controls.Add(Type:=msoControlButton). _ Caption = "Lock Excel" '//assign procedure to this control .Controls("Lock Excel"). _ OnAction = "LogOffPC" End With '//start the timer Run "StartTimer" End Sub Private Sub Workbook_SheetCalculate(ByVal Sh As Object) Run "DisableTimer" Run "StartTimer" End Sub Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Excel.Range) Run "DisableTimer" Run "StartTimer" End Sub Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean) Run "DisableTimer" Run "StartTimer" End Sub Private Sub Workbook_BeforeClose(Cancel As Boolean) '//disable timer Run "DisableTimer" '//remove the right-click control Run "RemoveFromMenu" End Sub '********************************************* '********************************************* '<< CODE FOR MODULE1 >> Option Explicit Option Compare Text Declare Function ExitWindowsEx& Lib "user32" _ (ByVal uFlags&, ByVal wReserved&) Global Const EWX_LOGOFF = 0 Public IdleTime As Date Sub StartTimer() '< Self-explanatory '********************************** '<< set your own idle time below >> IdleTime = Now + TimeValue("00:02:00") '********************************** Application.ontime IdleTime, "LogOffPC" End Sub Sub DisableTimer() '< Self-explanatory On Error Resume Next Application.ontime EarliestTime:=IdleTime, _ Procedure:="LogOffPC", Schedule:=False End Sub '// This example requires the user to type in a PIN to '// continue after the expiry of the idle time. The user '// is allowed 3 attempts to enter the correct PIN and '// if they fail the 3 times they will be logged-off, thus '// forcing the use of both; any Windows Log-in '// password, and any password that may be required '// to open the file. Sub LogOffPC() Dim MyPIN As String, Action&, N&, Win As Window Dim Book As Workbook, ThisBook As Workbook '******************************************* 'Set your own password below Const MyPassword As String = "1234" '******************************************* Set ThisBook = ActiveWorkbook 'hide any open books For Each Win In Windows Win.Visible = False Next Win N = 1 EnterPIN: Application.EnableCancelKey = xlDisabled Application.DisplayAlerts = False 'define the PIN as an input box entry MyPIN = Application.InputBox("Please enter PIN " & _ "to continue", "Time Expired - " & "Attempt " & N, "****") '--------------------------------------------------------------- 'if PIN is incorrect If MyPIN <> MyPassword Then 'if less than 3 attempts have another go If N < 3 Then N = N + 1 GoTo EnterPIN Else 'otherwise - log-off MsgBox "It appears you've no authority to" & vbLf & _ "use this workbook - Logging off", , "Log-off PC" 'remove right-click menu item RemoveFromMenu 'unhide open books For Each Win In Windows Win.Visible = True If Win.Caption = "Personal.xls" Then Win.Close Next Win 'save any open books For Each Book In Workbooks Book.Save Next Book '//log-off Application.DisplayAlerts = False Action = ExitWindowsEx(EWX_LOGOFF, 0&) Application.Quit End If '--------------------------------------------------------------- Else 'if PIN is correct, unhide open books For Each Win In Windows Win.Visible = True If Win.Caption = "Personal.xls" Then Win.Close Next Win 'activate the book ThisBook.Activate 'restart the timer StartTimer End If End Sub Private Sub RemoveFromMenu() On Error Resume Next '< error = no control '//remove right-click control With Application.CommandBars("Cell") .Controls("Lock Excel").Delete End With End Sub '*********************************************

How to use:

  1. Open an Excel workbook
  2. Select Tools/Macro/Visual Basic Editor
  3. In the VBE window, select Insert/Module
  4. Copy and paste the "code for the standard module" into this Module
  5. Select View/Project Explorer
  6. Select the ThisWorkbook module
  7. Copy and paste the "code for the ThisWorkbook module" to this Module
  8. Now select File/Close and Return To Microsoft Excel
  9. Don't forget to save your changes...
 

Test the code:

  1. Download the attachment and extract the enclosed workbook
  2. Open the workbook and wait two minutes
  3. When requested, enter the PIN 1234
  4. Wait two minutes
  5. When requested for a PIN, just click OK or Cancel three times (Your current Windows session will then be terminated)
 

Sample File:

LogoffAfterIdle.zip 17.83KB 

Approved by mdmackillop


This entry has been viewed 339 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express