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
![]() |
Patch reliability is unclear. Unless you have an immediate, pressing need to install a specific patch, don't do it. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » 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.
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
===========================================================
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
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
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 & "'")
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.