Results 1 to 10 of 10
  1. #1
    5 Star Lounger
    Join Date
    Jul 2001
    Location
    Terneuzen, Netherlands
    Posts
    895
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Win Eventlog in VB6 / VBA? (Office 2003 / VB6)

    Are there ways in either Excel2003 VBA or VB6 to read the WinXP Event Application-log? I want to check for errors and selectively report these.

    Erik Jan

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Win Eventlog in VB6 / VBA? (Office 2003 / VB6)

    See Event Logs. You'll find a whole bunch of VBScript code snippets to handle event logs. The scripts can be used in VB6 and VBA.

  3. #3
    5 Star Lounger
    Join Date
    Jul 2001
    Location
    Terneuzen, Netherlands
    Posts
    895
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Win Eventlog in VB6 / VBA? (Office 2003 / VB6)

    Cool, works. Got this code in a script to test and it works (the filename is there only to see results in a file i.s.o. on screen).

    Now I want to do the filering and reporting in VB6, how is that done (template or example is all I need)

    ================================================== =========

    Const ForWriting = 2

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objTextFile = objFSO.OpenTextFile _
    ("d:service_status.txt", ForWriting, True)


    strComputer = "."
    Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!" & strComputer & "rootcimv2")

    Set colLoggedEvents = objWMIService.ExecQuery _
    ("Select * from Win32_NTLogEvent Where Logfile = 'Application' and " _
    & "Type = 'Error'")

    For Each objEvent in colLoggedEvents
    objTextFile.WriteLine("Category: " & vbTab & objEvent.Category)
    objTextFile.WriteLine("Computer Name: " & vbTab & objEvent.ComputerName)
    objTextFile.WriteLine("Event Code: " & vbTab & objEvent.EventCode)
    objTextFile.WriteLine("Message: " & vbTab & objEvent.Message)
    objTextFile.WriteLine("Number: " & vbTab & objEvent.RecordNumber)
    objTextFile.WriteLine("Source Name: " & vbTab & objEvent.SourceName)
    objTextFile.WriteLine("Time Written: " & vbTab & objEvent.TimeWritten)
    objTextFile.WriteLine("Event Type: " & vbTab & objEvent.Type)
    objTextFile.WriteLine("User: " & vbTab & objEvent.User)
    Next
    objTextFile.Close

    Wscript.Echo "Total events written: " & colLoggedEvents.Count

    ================================================== =========

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Win Eventlog in VB6 / VBA? (Office 2003 / VB6)

    Sorry, I don't understand the question. Could you elaborate?

  5. #5
    5 Star Lounger
    Join Date
    Jul 2001
    Location
    Terneuzen, Netherlands
    Posts
    895
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Win Eventlog in VB6 / VBA? (Office 2003 / VB6)

    Sorry, this was a VBS-script. I believed it required something to make it work in Excel or VB6. Your response seemed to indicate this would work as listed; I just tried and indeed it works. My mistake, I'm sorry <img src=/S/blackteeth.gif border=0 alt=blackteeth width=20 height=20>

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Win Eventlog in VB6 / VBA? (Office 2003 / VB6)

    VBScript is just a slightly simplified version of VBA. You should be able to run VBScript code in VB6 or VBA without problems, as you found.

  7. #7
    5 Star Lounger
    Join Date
    Jul 2001
    Location
    Terneuzen, Netherlands
    Posts
    895
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Win Eventlog in VB6 / VBA? (Office 2003 / VB6)

    OK, now that this sort of works, I'd like to get e.g. only the messages out from the last day. How to I adjust the query below to do that; I tried stuff with "Time=" but cannot get to the proper syntax.

    Set colLoggedEvents = objWMIService.ExecQuery _
    ("Select * from Win32_NTLogEvent Where Logfile = 'Application' and Type = 'Error'")

    Any suggestion is appreciated.

    EJ

  8. #8
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Win Eventlog in VB6 / VBA? (Office 2003 / VB6)

    Look at your own example higher up in this thread. The relevant field is TimeWritten.
    Also see these examples from the site I referred to: List Events For a Specific Day From An Event Log and <A target="_blank" HREF="http://www.microsoft.com/technet/scriptcenter/scripts/logs/eventlog/lgevvb04.mspx">Copy the Previous Day

  9. #9
    5 Star Lounger
    Join Date
    Jul 2001
    Location
    Terneuzen, Netherlands
    Posts
    895
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Win Eventlog in VB6 / VBA? (Office 2003 / VB6)

    OK, thanks, now I get an invalid query (sorry that I'm not picking this one up better...):

    Dim dtmStartDate As Date, dtmEndDate As Date
    '
    dtmEndDate = Now()
    dtmStartDate = Now() - 1
    '
    Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonat e}!.rootcimv2")
    Set colLoggedEvents = objWMIService.ExecQuery _
    ("Select * from Win32_NTLogEvent Where Logfile = 'Application' and " _
    & "Type = 'Error' and TimeWritten >= '" & dtmStartDate & "' and TimeWritten < '" & dtmEndDate & "'")

  10. #10
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Win Eventlog in VB6 / VBA? (Office 2003 / VB6)

    Study the sample scripts to see how dates are handled.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •