Das Ganze sieht jetzt so aus :):
Private Function ReadEvents(Optional CompName As String = ".", _
Optional SheetName As String = "Eventlogdaten", _
Optional UserName As String = "", _
Optional UserPass As String = "") As Integer
Dim printEvents As Object
Dim printEvent As Variant
Dim EventData As Collection
Dim EventStructure As String
Dim objSWbemLocator As Object
Dim objWMIService As Object
Dim myWS As Worksheet
Dim offset As Long
Set myWS = Worksheets(SheetName)
Worksheets(SheetName).Range("A:I").ClearContents
On Error GoTo ErrHand
Application.StatusBar = "Initializing WMI..."
Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")
Set objWMIService = objSWbemLocator.ConnectServer(CompName, "\root\cimv2", UserName, UserPass)
Application.StatusBar = "Querying destination Event Log..."
Set printEvents = objWMIService.ExecQuery("Select * from Win32_NTLogEvent WHERE EventCode=10 And SourceName='Microsoft-Windows-PrintSpooler'")
On Error GoTo 0
myWS.Cells(1, 1) = "Zeitpunkt"
myWS.Cells(1, 2) = "Zeitquantum"
myWS.Cells(1, 3) = "Dokumentnummer"
myWS.Cells(1, 4) = "Dokumentname"
myWS.Cells(1, 5) = "Besitzer"
myWS.Cells(1, 6) = "Anschluss"
myWS.Cells(1, 7) = "Drucker"
myWS.Cells(1, 8) = "Größe"
myWS.Cells(1, 9) = "Seiten"
offset = 2
Application.StatusBar = "Processing events..."
For Each printEvent In printEvents
myWS.Cells(offset, 1) = DateValue(Format$( _
Left$(printEvent.TimeGenerated, 14), _
"&&&&-&&-&& &&:&&:&&")) + _
TimeValue(Format$( _
Left$(printEvent.TimeGenerated, 14), _
"&&&&-&&-&& &&:&&:&&"))
myWS.Cells(offset, 2).Formula = "=Date(Year(A" & offset & ")," _
& "Month(A" & offset & "),1)"
myWS.Cells(offset, 3) = CLng(printEvent.InsertionStrings(0))
myWS.Cells(offset, 4) = "'" & printEvent.InsertionStrings(1)
myWS.Cells(offset, 5) = "'" & printEvent.InsertionStrings(2)
myWS.Cells(offset, 6) = "'" & printEvent.InsertionStrings(4)
myWS.Cells(offset, 7) = "'" & printEvent.InsertionStrings(3)
myWS.Cells(offset, 8) = CLng(printEvent.InsertionStrings(5))
myWS.Cells(offset, 9) = CLng(printEvent.InsertionStrings(6))
offset = offset + 1
If (offset - 2) Mod 100 = 0 Then
Application.StatusBar = offset - 2 & " Events processed..."
End If
Next
Application.StatusBar = "Done processing " & offset - 2 & " events"
ReadEvents = 0
Exit Function
ErrHand:
MsgBox "Bei der Verarbeitung der WMI-Abfrage ist ein Fehler aufgetreten:" & vbCrLf & _
Err.Description, vbExclamation, "Fehler bei Verarbeitung"
ReadEvents = -1
End Function