AW: Pfadprüfung in Workbook_Open
21.06.2012 10:14:48
fcs
Hallo Marc,
mit nachfolgenden Anpassungen sollte es funktionieren.
Ich gehe davon aus, dass in Sheet 2, Zelle "I21", das Verzeichnis für das Logfile in der Form "D:\Test\LOG-File\" steht.
Gruß
Franz
Sub UnauthorizedActions_Msg()
Dim strPath As String, strEntry As String, strHead As String
Dim FF As Integer
' log file headers
strHead = "Date" & Space$(Len(Format(Date, "YYYY/MM/DD")) - 2) & _
"Time" & Space$(Len(CStr(Time)) - 2) & _
"User"
'Verzeichnes des Log-Files
strPath = Sheets(2).Range("I21")
If Right(strPath, 1) = Application.PathSeparator Then
'Backslash "\" abtrennen
strPath = Left(strPath, Len(strPath) - 1)
End If
' prüfen, ob log-Verzeichnis existiert
If Dir(strPath, vbDirectory) = "" Then
'Logfile im Verzeichnis der Datei anlegen
strPath = ActiveWorkbook.Path
End If
'Pfad & Name der Datei
strPath = strPath & Application.PathSeparator & "Unauthorized-Access.txt"
' checks if log file already exists and creates new one if not yet available
If Dir(strPath) = "" Then
FF = FreeFile
Open strPath For Append As #FF
Print #FF, strHead
Close #FF
End If
' log file content
strEntry = Format(Date, "YYYY/MM/DD") & Space$(2) & _
Time & Space$(2) & _
Environ$("UserName") & Space$(2)
FF = FreeFile
' adds text to log file
Open strPath For Append As #FF
Print #FF, strEntry
Close #FF
Const bytZeit As Byte = 5 'time limit for automatic closure
Dim objWSH As Object, intMSG As Integer
Set objWSH = CreateObject("WScript.Shell")
intMSG = objWSH.Popup("Unauthorized actions reported - No permissions to save or send file ! _
", _
bytZeit, "Unauthorized Access !", vbCritical)
Set objWSH = Nothing
ActiveWorkbook.Close False
End Sub