PDA

View Full Version : Log Off or Lock Computer = Save/Exit Workbook



wrightyrx7
03-01-2012, 07:59 AM
Hi all,

I think the thread name says it all. Is it possible to save and exit a workbook if the person using the workbook logs off or locks the computer?

This would be a great help.

I tried running a timer to save exit a workbook, but I noticed the timer works but the workbook wont close till the user logs back in


Regards
Chris

I found the code below that could possible to tinkered with?


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


Sub Test
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
End Sub