HI
Bin schon ganz weit nur der aufruf klappt nicht
Hab mir jetzt folgenden Code zusammen geschustert:
Modul3
Declare Function CallWindowProc Lib "user32" Alias _
"CallWindowProcA" (ByVal lpPrevWndFunc As Long, _
ByVal hWnd As Long, ByVal MSG As Long, _
ByVal wParam As Long, ByVal lParam As Long) As Long
Declare Function SetWindowLong Lib "user32" Alias _
"SetWindowLongA" (ByVal hWnd As Long, _
ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public Declare Function WTSRegisterSessionNotificationA Lib "WtsApi32.dll" _
Alias _
"WTSRegisterSessionNotification" (ByVal hWnd As Long, ByVal dwFlags As _
Long) As Long
Public Declare Function WTSUnRegisterSessionNotificationA Lib "WtsApi32.dll" _
Alias _
"WTSUnRegisterSessionNotification" (ByVal hWnd As Long) As Long
Private Const GWL_WNDPROC As Long = -4&
Private Const WM_WTSSESSION_CHANGE As Long = &H2B1
Private Const WTS_SESSION_LOCK As Long = 7
Private Const WTS_SESSION_UNLOCK As Long = 8
Public Const WTS_SESSION_LOGON As Long = 5
Public Const WTS_SESSION_LOGOFF As Long = 6
Private Const NOTIFY_FOR_THIS_SESSION As Long = &H0
Private gHW As Long
Private lpPrevWndProc As Long
Public Sub Hook(hWnd As Long)
gHW = hWnd
lpPrevWndProc = SetWindowLong(gHW, GWL_WNDPROC, AddressOf WindowProc)
Call WTSRegisterSessionNotificationA(hWnd, NOTIFY_FOR_ALL_SESSIONS)
End Sub
Public Sub Unhook()
Call SetWindowLong(gHW, GWL_WNDPROC, lpPrevWndProc)
Call WTSUnRegisterSessionNotificationA(gHW)
End Sub
Function WindowProc(ByVal hw As Long, ByVal uMsg As Long, _
ByVal wParam As Long, ByVal lParam As Long) As Long
Dim ms As String
Select Case uMsg
Case WM_WTSSESSION_CHANGE
Select Case wParam
Case WTS_SESSION_LOCK
'Sitzuung wird gesperrt
ms = MsgBox("You are locking your computer," & _
"DNOX2-Befundungsliste.xls will be closed!", _
vbOKOnly, "Caution!")
ThisWorkbook.Save ' Datei speichern
' falls nur eine Datei auf Excel schließen
If Workbooks.Count = 1 Then Application.Quit Else ThisWorkbook.Close
Case WTS_SESSION_UNLOCK
'Sitzung wird entsperrt
'...
End Select
End Select
'Auf jeden Fall die Standardverarbeitung aufrufen
WindowProc = CallWindowProc(lpPrevWndProc, hw, uMsg, wParam, lParam)
End Functionund in der Arbeitsmappe
Private Sub Workbook_Open()
If ThisWorkbook.ReadOnly = False Then
Call Hook
End If
End Sub Sollte doch eigentlich funktionieren oder?
Aber es kommt er Fehler Argument ist nicht optional
Der fehler lieg wahrscheinlich im aufruf von hook aber wie mach ich das ganze dann richtig?? |