For what it is worth column. Built the following VBScript app that runs on one of my servers to create an Excel spreadsheet based on ADO queries against an Access MDB.
To be sure the following code could be slimmed down, but this is simply a sample.
For what it is worth column.
DougCranston
'~~ScriptName~~. (rpthd.vbs)
'----------------------------------------------------
'~~Author~~. DCranston
'~~Prerequisites~~. VBScript 5.0
'~~Version~~. 1.0
'~~Date Created~~. 2/6/2003
'~~Version_History~~. Initial version
'~~Keywords~~. File exists, FileSystemObject
'~~Description~~. Test if a folder exists and creates one if not.
' Tests for file exists and deletes if exists.
' Creates an Excel spread, extracts data from
' Access MDB via ADO and DSN. Formats the spreadsheet
' saves File.
'-----------------------------------------------------
'~~Usage: rptHD [optional <startdate><enddate>
'~~[No Optional Arguments]: Generates report for last month.
'~~<startdate>: First day of month. Format mm/dd/yyyy
'~~<enddate>: Last day of month under report. Format mm/dd/yyyy
'========= Declarations =================
OPTION EXPLICIT
' Dimension variables
Dim dtFirstDayofLastMonth
Dim dtLastDayofLastMonth
Dim strFile
Dim strFilenm
Dim strfldr
Dim fndt
Dim i
dim strTxt
Dim rs1
Dim rs2
Dim sql
Dim sql1
Dim fldmax
Dim rowmax
Dim rowmax2
Dim iCol
Dim iRow2
Dim iRow
Dim recArray
Dim recCount
Dim result
dim strEndRptDt
dim strStrtRptDt
dim argsvalue
dim argtst
dim numerr
dim abouterr
dim errloc
dim strErrCd
dim errmessage
dim strErrLogFldr
dim strErrLogFn
dim strScriptNm
dim ii
dim strErrMsg
dim strErrTitle
dim dt
dim tdt
dim intDate
dim intDay
dim intMonth
dim intYear
dim oNewFldr
Dim oCn
dim oFilesys
Dim oWshell
Dim oArgs
Dim oXLApp
Dim oXLWb
Dim oXLWs
Dim oMessage
Dim oErrFile
Dim otxtstream
dim ecode
' Create Objects
Set oWShell = WScript.CreateObject("WScript.Shell"

Set oArgs = WScript.Arguments
' Assign Values to variables
strScriptNm = "RPTHD"
strFilenm = "rpthd.xls"
strFldr = "c:\cmdfiles\tempfile"
strFile = strFldr & "\" & strFilenm
strErrLogFldr = "c:\cmdfiles\logs"
strErrLogFn = "rcerpthd.log"
call CreateFnm
call CreateRpt
'---------------------------
' Functions and Subroutines
'---------------------------
Public Function CreateFnm()
' Set Error Handler
On Error Resume Next
argsvalue = oArgs.Count
If oArgs.Count = 0 Then
' Has no Args - assumes batch/cron run
' Call create directory routine
Call CreateDir()
' Call clearfile routine
Call ClearFile()
' Calculate start of past month and end of month
dtFirstDayofLastMonth = DateSerial(Year(Date), Month(Date) - 1, 1)
dtLastDayofLastMonth = DateSerial(Year(Date), Month(Date), 0)
set oArgs = nothing
ElseIf oArgs.Count = 2 Then
' Has two Command line arguments - For running other months reports
' Call create directory routine
Call CreateDir
' Test for valid date entered at command line
if isdate(oArgs.Item(0)) then
dtFirstDayofLastMonth = oArgs.Item(0)
Else
Err.Raise vbObjectError + 16 , ,"CustomErr - Invalid date."
strErrCd = "First date entered is not a valid date."
call LogErrors(Err, strErrCd)
wscript.quit(1)
end if
' Test for valid date entered at command line
if isdate(oArgs.Item(1)) then
dtLastDayofLastMonth = oArgs.Item(1)
else
Err.Raise vbObjectError + 16 , ,"CustomErr - Invalid date."
strErrCd = "Second date entered is not a valid date."
call LogErrors(Err, strErrCd)
wscript.quit(1)
end if
' Create filename based on date
tdt = cdate(dtLastDayofLastMonth)
fndt = show_date(tdt)
strFilenm = "rce" & fndt & ".xls"
set oArgs = nothing
Else
' If we got here we have incorrect # of commandline arguments.
set oArgs = nothing
strErrCd = "Incorrect # of commandline arguments."
Err.Raise 450
call LogErrors(Err, strErrCd)
wscript.quit(1)
End If
' Assigns datebased filename to use to name spreadsheet.
CreateFnm=strFilenm
end Function
function show_date(dt)
' Function to convert date to mmyy with 0 padding
' Set Error Handler
On Error Resume Next
' Initialize local variables
intDate = dt
intDay = Day(intDate)
intMonth = Month(intDate)
intYear = Year(intDate)
'Get the day : parse method
if Len(intDay)=1 Then intDay = "0" & intDay
'Get the month : parse method
if Len(intMonth)=1 Then intMonth = "0" & intMonth
'get the year : parse method
if Len(intYear)=4 Then intYear = Right(intYear, 2)
show_date = intMonth & intYear
'Test for Error
If Err.Number <> 0 then
Err.Raise vbObjectError + 17 , ,"CustomErr - Process failed."
strErrCd = "Generated filename based on date process failed."
call LogErrors(Err, strErrCd)
wscript.quit(1)
End if
End function
Public Sub CreateDir()
' Test to see if Folder Exists and if not create it
' Set Error Handler
On Error Resume Next
' Test and create script output folder
Set oFilesys = CreateObject("Scripting.FileSystemObject"

If Not oFilesys.FolderExists(strFldr) Then
Set oNewFldr = oFilesys.CreateFolder(strFldr)
Set oNewFldr = Nothing
End If
' Test and create script ERROR LOG folder
If Not oFilesys.FolderExists(strErrLogFldr) Then
Set oNewFldr = oFilesys.CreateFolder(strErrLogFldr)
Set oNewFldr = Nothing
End If
set oFilesys = Nothing
'Test for Error
If Err.Number <> 0 then
Err.Raise vbObjectError + 18 , ,"CustomErr - Create Dir failed."
strErrCd = "Creating Subdirectory process failed."
call LogErrors(Err, strErrCd)
wscript.quit(1)
End if
End Sub
Public Sub ClearFile()
' Test if Old version of file exists delete it
' Set Error Handler
On Error Resume Next
Set oFilesys = CreateObject("Scripting.FileSystemObject"

if oFilesys.FileExists(strFile) then
oFilesys.DeleteFile(strFile)
end if
set oFilesys = Nothing
'Test for Error
If Err.Number <> 0 then
strErrCd = "Creating Subdirectory process failed."
call LogErrors(Err, strErrCd)
wscript.quit(1)
End if
End Sub
Sub CreateRpt()
' Connect to the db with a DSN-less connection
' Set Error Handler
On Error Resume Next
' --- Create Instance of Connection Object ---
Set oCn = Wscript.CreateObject("ADODB.Connection"

oCn.Open "DSN=rcerpt" 'DC home
' oCn.Open "DSN=rptrqst" ' dc dsktop2000 ofc
' Create a server recordset object
Set rs1 = Wscript.CreateObject("ADODB.Recordset"
' Define Query to determine select criteria
sql1 = "SELECT DISTINCT tblStores.txtRegion "
sql1 = sql1 & "FROM tblStores "
sql1 = sql1 & "ORDER BY tblStores.txtRegion;"
' Execute the sql
rs1.Open sql1, oCn, 3, 3
'this opens a recordset with all unique names in Field
If Not rs1.EOF Then
rs1.MoveLast
rs1.movefirst
rowmax2 = rs1.RecordCount
End If
' Create an instance of Excel and add a workbook
Set oXLApp = CreateObject("Excel.Application"

'this required to determine recordcount
oXLApp.SheetsInNewWorkbook = rs1.RecordCount
'create a workbook with required sheets number
Set oXLWb = oXLApp.Workbooks.Add
'cycle thru sheets
For i = 1 To rowmax2
Set oXLWs = oXLWb.Worksheets(i)
oXLWs.Name = rs1.Fields(0).Value
' Create recordset obj
Set rs2 = Wscript.CreateObject("ADODB.Recordset"
' Define main query
sql = "SELECT DISTINCTROW tblStores.txtStoreNo, "
sql = sql & "tblStores.txtStoreRCECd, tblStores.txtRegion, "
sql = sql & "tblStores.txtStoreName, "
sql = sql & "tblStores.txtState, "
sql = sql & "tblRequests.dtRqstSubmitted, "
sql = sql & "tblRequests.dtRptDate, tblRequests.txtRptType, "
sql = sql & "LCase([txtRequestor]), "
sql = sql & "tblRequests.txtBatchNo, "
sql = sql & "LCase([txtRqstReason])"
sql = sql & "FROM tblRequests INNER JOIN tblStores "
sql = sql & "ON tblRequests.txtStoreRCECd = tblStores.txtStoreRCECd "
sql = sql & "WHERE (((tblRequests.dtRqstSubmitted) Between #" & dtFirstDayofLastMonth & "# And #" & dtLastDayofLastMonth & "#)) "
sql = sql & "AND tblStores.txtRegion = '" & rs1.Fields(0).Value & "' "
sql = sql & "ORDER BY tblStores.txtRegion, "
sql = sql & "tblStores.txtStoreNo, "
sql = sql & "tblRequests.dtRqstSubmitted;"
' Execute the sql
rs2.Open sql, oCn, 3, 3
' Call function to process request
Call TXLOut(sql, rs1.Fields(0).Value)
' Clean up Recordset
rs2.Close
Set rs2 = Nothing
' Next Record
rs1.movenext
Next 'i
' Close and set the recordset to nothing
rs1.Close
Set rs1 = Nothing
' Close and set the connection to nothing
oCn.Close
Set oCn = Nothing
' Save the spreadsheet as
oXLWb.SaveAs (strFldr & "\" & createFnm)
Set oXLWs = Nothing
oXLWb.Close False '/False to ignore changes '/ True to save changes
Set oXLWb = Nothing
oXLApp.Quit
Set oXLApp = Nothing
Set oFilesys = Nothing
' Send completed spreadsheet via email to assigned users
' Call sndfilemsg(dtFirstDayofLastMonth, dtLastDayofLastMonth, argsvalue)
'Test for Error
If Err.Number <> 0 then
strErrCd = "Creating Report process failed."
call LogErrors(Err, strErrCd)
wscript.quit(1)
End if
End Sub
Public Function TXLOut(sql,sheet)
'Notice, that you need References to ADO
' Set Error Handler
On Error Resume Next
' Block display Excel and user control of Excel's lifetime
' FOR TESTING SET BOTH TO TRUE set to False to run scripted/crond
oXLApp.Visible = FALSE
oXLApp.UserControl = FALSE
' Test and address empty recordset
if rs2.BOF and rs2.EOF then
' Add Indication of records
oXLWs.Cells(2 , 9).Value = "No Report Requests For This Time Period"
With oXLWs.Range("I2"

.Interior.Color = RGB(196,196,196)
End With
Else
' Determine the array we are dealing with
If Not rs2.BOF And Not rs2.EOF Then
rs2.MoveFirst()
rs2.MoveLast()
rowmax = rs2.RecordCount + 1
fldmax = rs2.Fields.Count
End If
' Move to first record in recordset
rs2.movefirst
' Set counter for positioning data posting
iRow2 = 2
' Start a for/next loop that will end with the last record
For iRow = iRow2 To rowmax
For iCol = 1 To fldmax
If (rs2.Fields.Item(iCol - 1) = ""

Then
oXLWs.Cells(iRow, iCol).Value = ""
Else
oXLWs.Cells(iRow, iCol).Value = rs2.Fields(iCol - 1).Value
End If
Next
iRow2 = iRow2 + 1
' Move to the next record
rs2.movenext
Next
' Add Indication of records
oXLWs.Cells(rowmax + 1, 9).Value = "Total Requests"
oXLWs.Cells(rowmax + 1, 11).Value = rowmax - 1
' Set shading for column titles
With oXLWs.Range("A" & rowmax +1 &":" & PickCol(fldmax) & rowmax +1)
.Interior.Color = RGB(196,196,196)
End With
End If
' Set field names to the first row of the worksheet
oXLWs.Cells(1, 1).Value = "Store#"
oXLWs.Cells(1, 2).Value = "Cd"
oXLWs.Cells(1, 3).Value = "Region"
oXLWs.Cells(1, 4).Value = "Store Name"
oXLWs.Cells(1, 5).Value = "State"
oXLWs.Cells(1, 6).Value = "Dt Sbmtd"
oXLWs.Cells(1, 7).Value = "Rprt Dt"
oXLWs.Cells(1, 8).Value = "Rpt Type"
oXLWs.Cells(1, 9).Value = "Requestor"
oXLWs.Cells(1, 10).Value = "Batch #"
oXLWs.Cells(1, 11).Value = "Reason"
' Set Column Widths
oXLWs.Columns(1).ColumnWidth = 5.57
oXLWs.Columns(2).ColumnWidth = 5.57
oXLWs.Columns(3).ColumnWidth = 4
oXLWs.Columns(4).ColumnWidth = 19.75
oXLWs.Columns(5).ColumnWidth = 4
oXLWs.Columns(6).ColumnWidth = 9.3
oXLWs.Columns(7).ColumnWidth = 9.3
oXLWs.Columns(8).ColumnWidth = 8.3
oXLWs.Columns(9).ColumnWidth = 16.86
oXLWs.Columns(10).ColumnWidth = 5.57
oXLWs.Columns(11).ColumnWidth = 25
' Set first row as column titles over mult pages
With oXLWs.PageSetup
.PrintTitleRows = "$1:$1"
End With
oXLWs.PageSetup.PrintArea = ""
' Set Page header and footers, page margins, layout
oXLWs.PageSetup.PrintArea = ""
With oXLWs.PageSetup
.LeftHeader = ""
.CenterHeader = "Reprint Requests by Region/Store#/Date Request Submitted for Period: " & dtFirstDayofLastMonth & "-" & dtLastDayofLastMonth
.RightHeader = ""
.LeftFooter = "&F"
.CenterFooter = "Page - &P"
.RightFooter = "&D &T"
.LeftMargin = oXLApp.InchesToPoints(0.5)
.RightMargin = oXLApp.InchesToPoints(0.5)
.TopMargin = oXLApp.InchesToPoints(0.8)
.BottomMargin = oXLApp.InchesToPoints(0.8)
.HeaderMargin = oXLApp.InchesToPoints(0.5)
.FooterMargin = oXLApp.InchesToPoints(0.5)
.PrintHeadings = False
.PrintGridlines = False
.PrintComments = -4142
.Orientation = 2 ' landscape
.PrintQuality = 600
.CenterHorizontally = False
.CenterVertically = False
.Draft = False
.PaperSize = 1 ' xlPaperLetter
.FirstPageNumber = 1 ' xlAutomatic
.Order = 1 ' xlDownThenOver
.BlackAndWhite = False
.Zoom = 100
End With
' Set formatting for entire sheet
' Addition of a + 1 is to include formatting for the report total line
' oXLWs.Range("A1:" & PickCol(fldmax) & rowmax + 1).Select
oXLWs.Range("A1:" & PickCol(fldmax) & rowmax + 1).Font.Name = "Times New Roman"
oXLWs.Range("A1:" & PickCol(fldmax) & rowmax + 1).Font.Size = 8
oXLWs.Range("A1:" & PickCol(fldmax) & rowmax + 1).Font.Bold = True
oXLWs.Range("A1:" & PickCol(fldmax) & rowmax + 1).WrapText = True
' Set shading for column titles
With oXLWs.Range("A1:" & PickCol(fldmax) & "1"

.Interior.Color = RGB(196,196,196)
End With
' Set lines around cells
With oXLWs.Range("A1:" & PickCol(fldmax) & rowmax)
with .Borders(7)
.LineStyle = 1
.Weight = 2
.Color = RGB(0,0,0)
end with
with .Borders(8)
.LineStyle = 1
.Weight = 2
.Color = RGB(0,0,0)
end with
with .Borders(9)
.LineStyle = 1
.Weight = 2
.Color = RGB(0,0,0)
end with
with .Borders(10)
.LineStyle = 1
.Weight = 2
.Color = RGB(0,0,0)
end with
with .Borders(11)
.LineStyle = 1
.Weight = 2
.Color = RGB(0,0,0)
end with
with .Borders(12)
.LineStyle = 1
.Weight = 2
.Color = RGB(0,0,0)
end with
End With
'Test for Error
If Err.Number <> 0 then
strErrCd = "Creating Report process failed."
call LogErrors(Err, strErrCd)
wscript.quit(1)
End if
End Function
Function PickCol(fldmax)
' Translates cell column # to Alpha Char for cell ref
Select Case fldmax
Case 1
result = "A"
Case 2
result = "B"
Case 3
result = "C"
Case 4
result = "D"
Case 5
result = "E"
Case 6
result = "F"
Case 7
result = "G"
Case 8
result = "H"
Case 9
result = "I"
Case 10
result = "J"
Case 11
result = "K"
Case 12
result = "L"
Case 13
result = "M"
Case 14
result = "N"
Case 15
result = "O"
Case 16
result = "P"
Case 17
result = "Q"
Case 18
result = "R"
Case 19
result = "S"
End Select
PickCol = result
End Function
Function LogErrors(objErr,strErrCd)
' Function writes information to an Error log file and
' generates an email warning to server administrators
'
errloc = strErrCd
numerr = Err.Number
abouterr = Err.Description
strErrTitle = "Error in: " & strScriptNm & " Error#: " & numerr & " Type: " & abouterr
If numerr <> 0 Then
Set oErrFile = CreateObject("Scripting.FileSystemObject"

Set otxtstream = oErrFile.OpenTextFile(strErrLogFldr & "\" & strErrLogFn, 8, TRUE)
otxtstream.writeline("START Error log Page: " & " Date: " & Now() & " Err.Number: " & Err.Number)
' Determine whether a VBS error has occurred and log it
If Err.Number <> 0 Then
' Report properties of VBScript Err object and write to logfile
otxtstream.writeline("VBScript Err Object:"

otxtstream.writeline("Err Occurred at: " & strErrCd)
otxtstream.writeline("Number: " & Err.Number)
otxtstream.writeline("Description: " & Err.Description)
otxtstream.writeline("Source: " & Err.Source)
otxtstream.writeline("HelpFile: " & Err.HelpFile)
otxtstream.writeline("HelpContext: " & Err.HelpContext)
' Report propertiesof VBScript Err object for emailing report
strErrTitle = "Error in: "& strScriptNm & " Date: " & Now() & " Err.Number: " & Err.Number
strErrMsg = "VBScript Err Object:" & vbcrlf
strErrMsg = strErrMsg & "Err Occurred at: " & strErrCd & vbcrlf
strErrMsg = strErrMsg & "Number: " & Err.Number & vbcrlf
strErrMsg = strErrMsg & "Description: " & Err.Description & vbcrlf
strErrMsg = strErrMsg & "Source: " & Err.Source & vbcrlf
strErrMsg = strErrMsg & "HelpFile: " & Err.HelpFile & vbcrlf
strErrMsg = strErrMsg & "HelpContext: " & Err.HelpContext & vbcrlf & vbcrlf
End if
End if
' Determine whether an ADO Error has occurred and log it
If IsObject(oCn) Then
If oCn.Errors.Count > 0 Then
For ii = 0 To oCn.Errors.Count - 1
' Report properties of ADO Error object.
otxtstream.writeline("ADO Error Object:"

otxtstream.writeline("Number: " & oCn.Errors(ii).Number)
otxtstream.writeline("Description: " & oCn.Errors(ii).Description)
otxtstream.writeline("Source: " & oCn.Errors(ii).Source)
otxtstream.writeline("HelpFile: " & oCn.Errors(ii).HelpFile)
otxtstream.writeline("HelpContext: " & oCn.Errors(ii).HelpContext)
otxtstream.writeline("SQL State: " & oCn.Errors(ii).SQLState)
otxtstream.writeline("NativeError: " & oCn.Errors(ii).NativeError)
' End the range of the loop through all Error objects.
strErrMsg = strErrMsg & "ADO Error Object:" & vbcrlf
strErrMsg = strErrMsg & "Number: " & oCn.Errors(ii).Number & vbcrlf
strErrMsg = strErrMsg & "Description: " & oCn.Errors(ii).Description & vbcrlf
strErrMsg = strErrMsg & "Source: " & oCn.Errors(ii).Source & vbcrlf
strErrMsg = strErrMsg & "HelpFile: " & oCn.Errors(ii).HelpFile & vbcrlf
strErrMsg = strErrMsg & "HelpContext: " & oCn.Errors(ii).HelpContext & vbcrlf
strErrMsg = strErrMsg & "SQL State: " & oCn.Errors(ii).SQLState & vbcrlf
strErrMsg = strErrMsg & "NativeError: " & oCn.Errors(ii).NativeError & vbcrlf & vbcrlf
Next
End If
Set oCn = Nothing
End If
otxtstream.writeline("END Error log Page: " & " Date: " & Now() & vbcrlf & vbcrlf)
' Send Error Message to System Administrators of Problem Encountered
' If used on WIN2 Server/WS remove the double ''
' and then comment out the CDONTS
' Sending a text email using a CDOSYS not CDONTS
Set oMessage = CreateObject("CDO.Message"
oMessage.From = "fromuser@from.com"
oMessage.To = "touser@to.com"
oMessage.Subject = strErrTitle & " Scheduled Task"
oMessage.Textbody = strErrMsg
oMessage.Configuration.Fields.Item _
("
= 2
oMessage.Configuration.Fields.Item _
("
= _
"yoursmtpmailserver url"
oMessage.Configuration.Fields.Item _
("
= 25
oMessage.Configuration.Fields.Update
oMessage.Send
Set oMessage=Nothing
' CDONTS email for NT
' If using CDONTS remove double single quotes and comment out CDOSYS lines
'' set oMessage = CreateObject("CDONTS.NewMail"
'' oMessage.Subject = strErrTitle & " Scheduled Task"
'' oMessage.From = "fromuser@from.com"
'' oMessage.To = "touser@to.com"
'' oMessage.Cc = "ccuser@cc.com"
'' oMessage.Body = strErrMsg & VbCrLf
'' oMessage.Send
'' set oMessage=nothing
Wscript.Quit(1)
End Function
Public Function sndfilemsg(dtFirstDayofLastMonth, dtLastDayofLastMonth, argsvalue)
' Emails the report to the assigned personnel
' Set Error Handler
On Error Resume Next
strStrtRptDt = dtFirstDayofLastMonth
strEndRptDt = dtLastDayofLastMonth
argtst = cint(argsvalue)
If argtst = 0 then
' Send Error Message to System Administrators of Problem Encountered
' If used on WIN2 Server/WS remove the double ''
' and then comment out the CDONTS
' Sending a text email using a CDOSYS not CDONTS
Set oMessage = CreateObject("CDO.Message"
oMessage.From = "fromuser@from.com"
oMessage.To = "touser@to.com"
oMessage.Subject = "Report Requests for: " & strStrtRptDt & "-" & strEndRptDt
oMessage.AddAttachment "c:\cmdfiles\tempfile\rcerpthd.xls"
oMessage.TextBody = "The attached Excel file contains last months Reprint Requests Summary." & VbCrLf
oMessage.Configuration.Fields.Item _
("
= 2
oMessage.Configuration.Fields.Item _
("
= _
"yoursmtpmailaddress"
oMessage.Configuration.Fields.Item _
("
= 25
oMessage.Configuration.Fields.Update
oMessage.Send
Set oMessage=Nothing
' CDONTS email for NT
' If using CDONTS remove double single quotes and comment out CDOSYS lines
'wscript.echo strStrtRptDt
'wscript.echo strEndRptDt
'' set oMessage = CreateObject("CDONTS.NewMail"
'' oMessage.Subject = "Report Requests for: " & strStrtRptDt & "-" & strEndRptDt
'' oMessage.From = "fromuser@verizon.com"
'' oMessage.To = "touser@to.com"
'' oMessage.Cc = "ccuser@ccuser.com"
'' oMessage.AttachFile("c:\cmdfiles\tempfile\rcerpthd.xls"

'' oMessage.Body = "The attached Excel file contains last months Reprint Requests Summary." & VbCrLf
'' oMessage.Send
'' set oMessage=nothing
End if
'Test for Error
If Err.Number <> 0 then
strErrCd = "Creating Email distribution process failed."
call LogErrors(Err, strErrCd)
wscript.quit(1)
End if
End Function