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

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Win Eventlog in VB6 / VBA? (Office 2003 / VB6)

    Author
    Topic
    #441088

    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

    Viewing 0 reply threads
    Author
    Replies
    • #1058178

      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.

      • #1058205

        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

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

        • #1058208

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

          • #1058341

            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 blackteeth

            • #1058345

              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.

        • #1058477

          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

          • #1058482

            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 Copy the Previous Day

            • #1058574

              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=impersonate}!.rootcimv2”)
              Set colLoggedEvents = objWMIService.ExecQuery _
              (“Select * from Win32_NTLogEvent Where Logfile = ‘Application’ and ” _
              & “Type = ‘Error’ and TimeWritten >= ‘” & dtmStartDate & “‘ and TimeWritten < '" & dtmEndDate & "'")

            • #1058576

              Study the sample scripts to see how dates are handled.

    Viewing 0 reply threads
    Reply To: Reply #1058341 in Win Eventlog in VB6 / VBA? (Office 2003 / VB6)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information:




    Cancel