"tiv" <jmattivi@xxxxxx> wrote in message
news:1922cdcc-2fce-4e08-8d34-c17d2ef10a4c@xxxxxx
> Sorry if the first post was vague. Basically, it's not working trying
> to populate the data into excel.
>
> It's bombing out at this line - strQuery = "SELECT dtmDate, dtmTime
> FROM" & text1 when trying to select the objects to populate into
> excel. I believe it's my for statement and how i'm trying to grab the
> variables from it to go to excel, but not positive on how to pipe them
> in.... Your problem has nothing to do with "piping values into Excel" and
it's not failing on the line you quote but on the subsequent line, namely:
objCommand.CommandText = strQuery
and for a simple reason: You never defined an object "objCommand".
Some other observations:
- Since you use "IntNumber" always as a string, you should change
IntNumber = 27 to
StrNumber = "27"
- Having a statement such as
Set strFile = objFso.CreateTextFile(strPath, True)
is highly confusing. "strFile" is an object, not a string.
- I grabbed some code from my box of sample scripts and merged
it with your own code. It shows you how you can extract events
from the Event Logger and write them into spreadsheet cells.
Option Explicit
Dim objExcel, objFso, objCommand, objFolder, objWMI, objItem
Dim strEventLog, oEvent, x
Dim text1, strFile, strQuery, strComputer, strFolder, strFileName
Dim strPath, strTimeWritten, dtmTimeWritten, dtmDate, dtmTime
Dim intEvent, strEventID, intRecordNum, colLoggedEvents
' --------------------------------------------------------
' Set the folder and file name
' Set numbers
strEventID = "27" ' Event ID Number
intRecordNum = 0
Set objExcel = CreateObject("Excel.Application")
' Create a new workbook.
objExcel.Visible = True
objExcel.Workbooks.Add
strComputer = "."
strFileName = "\Event" & strNumberID & ".xls"
strFolder = "C:\"
strPath = strFolder & strFileName
strEventLog = "'application' "
' -----------------------------------------------------
' Section to create folder and hold file.
Set objFso = CreateObject("Scripting.FileSystemObject")
If objFso.FolderExists(strFolder) Then
Set objFolder = objFso.GetFolder(strFolder)
Else
Set objFolder = objFso.CreateFolder(strFolder)
WScript.Echo "Folder created " & strFolder
End If
'Wscript.Echo " Press OK and Wait 30 seconds (ish)"
Set strFile = objFso.CreateTextFile(strPath, True)
Set objWMI = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
' -----------------------------------------
' Next section loops through ID properties
strFile.WriteLine("Date: " & ", Time: " & ", ComputerName: " _
& " EventCode: " & ", Message: ")
For Each objItem In colLoggedEvents
strTimeWritten = objItem.TimeWritten
dtmTimeWritten = CDate(Mid(strTimeWritten, 5, 2) & "/" & _
Mid(strTimeWritten, 7, 2) & "/" & Left(strTimeWritten, 4) _
& " " & Mid (strTimeWritten, 9, 2) & ":" & _
Mid(strTimeWritten, 11, 2) & ":" & Mid(strTimeWritten, 13, 2))
dtmDate = FormatDateTime(dtmTimeWritten, vbShortDate)
dtmTime = FormatDateTime(dtmTimeWritten, vbLongTime)
Set colLoggedEvents = objWMI.ExecQuery _
("Select * from Win32_NTLogEvent " _
& "Where Logfile = " & strEventLog _
& " and TimeWritten >= 20080901" _
& " and EventCode = " & strEventID)
x = 1
For Each oEvent In colLoggedEvents
objExcel.Cells(x, 1).Value = oEvent.message
objExcel.Cells(x, 2).Value = oEvent.TimeGenerated
x = x + 1
Next
Next
' Save the spreadsheet and close the workbook.
objExcel.ActiveWorkbook.SaveAs strExcelPath
objExcel.ActiveWorkbook.Close
' Quit Excel.
objExcel.Application.Quit
WScript.quit