I know this likely sounds simple but this was just dumped on me after an employee left. The current script writes an Excel file and they now want it to be a .csv. I am new to VBS and need some pointers. It would be nice if I could just change the file extension but know this will not give me the proper formatting. This is the current script:<\/p>\n
Option Explicit<\/p>\n
Dim fsObject, ipControlFile, fcFileCreator, dbAdvPRD, qbQuery<\/p>\n
Dim ipList, rsListResults, strText, intResult<\/p>\n
Set fsObject = CreateObject(“Scripting.FileSystemObject”)<\/p>\n
Set ipControlFile = New cIpostControlFile<\/p>\n
Set fcFileCreator = New cFileCreator<\/p>\n
Set dbAdvPRD = New cDataBase<\/p>\n
Set qbQuery = New cQueryBuilder<\/p>\n
Set ipControlFile.AppObject = CreateObject(“Excel.Application”)<\/p>\n
ipControlFile.Directory = fsObject.GetAbsolutePathName(“.”)<\/p>\n
ipControlFile.ControlFile = “WhatCounts List Counts.xls”<\/p>\n
fcFileCreator.Location = fsObject.GetAbsolutePathName(“.”)<\/p>\n
fcFileCreator.Folder = \"WhatCounts Out \" & DatePart(“m”,Now) & “-” & DatePart(“yyyy”,Now)<\/p>\n
'Begin Script<\/p>\n
MsgBox “Retrieving data. This may take several minutes.”, 64, “Monthly WhatCounts Update”<\/p>\n
If Not fsObject.FileExists(ipControlFile.FullPath) Then<\/p>\n
strText = “The \" & ipControlFile.ControlFile & \" file is not available.”<\/p>\n
strText = strText & “The file must be located in the same directory as this script.”<\/p>\n
MsgBox strText, 16, “Error - ControlFile Not Found”<\/p>\n
ExitScript()<\/p>\n
End If<\/p>\n
If fsObject.FolderExists(fcFileCreator.FullPath) Then<\/p>\n
strText = \"A folder with this datestamp already exists \"<\/p>\n
strText = strText & \"and contains \"<\/p>\n
strText = strText & fsObject.GetFolder(fcFileCreator.FullPath).Files.Count & \" file(s). \"<\/p>\n
strText = strText & \" Would you like to use a this folder?\"<\/p>\n
intResult = MsgBox(strText, 35, “WhatCounts Monthly Update - Ouput directory”)<\/p>\n
If intResult = 2 Then 'User selects cancel, exit script<\/p>\n
ExitScript()<\/p>\n
End If<\/p>\n
If intResult = 7 Then 'User selects no, use temp folder<\/p>\n
strText = \"WhatCounts Out Temp \" & DatePart(“m”,Now) & “-” & DatePart(“yyyy”,Now)<\/p>\n
strText = strText & \" \" & DatePart(“h”,now) & Datepart(“n”, Now) & DatePart(“s”, Now)<\/p>\n
fcFileCreator.Folder = strText<\/p>\n
End If<\/p>\n
If intResult = 6 Then 'User selects yes, use default folder<\/p>\n
strText = “The file(s) in this folder will be overwritten.”<\/p>\n
intResult = MsgBox (strText,49,“WhatCounts Monthly Update - Output directory”)<\/p>\n
If intResult = 2 Then 'User cancelled afterall<\/p>\n
ExitScript()<\/p>\n
End If<\/p>\n
MsgBox “This options is currently not supported.”,16,“Whatcounts Monthly Update - Ouput directory”<\/p>\n
ExitScript()<\/p>\n
End If<\/p>\n
End If<\/p>\n
ipControlFile.Open 'The controlFile exists and should be opened for processing<\/p>\n
'Once the file has been processed we can return the request objects<\/p>\n
For Each ipList In ipControlFile.ListRequests<\/p>\n
If Right(ipList.Pubs,2) <> “–” Then 'Active list requests, not commented out in the controlfile<\/p>\n
qbQuery.ProcessRequest ipList<\/p>\n
Set rsListResults = dbAdvPRD.ExecuteQuery(qbQuery.SqlText)<\/p>\n
ipList.RecordCount = rsListResults.RecordCount<\/p>\n
Set ipList.Data = rsListResults 'Add the results to the ipList object’s data property<\/p>\n
Set rsListResults = Nothing 'Release the temporary recordset<\/p>\n
Else 'Set the inactive request’s count to NULL<\/p>\n
ipList.RecordCount = NULL<\/p>\n
End If<\/p>\n
ipControlFile.WriteCounts ipList.Pubs, ipList.lType, ipList.SubType, ipList.RecordCount 'Write the counts<\/p>\n
Next<\/p>\n
ipControlFile.DateStampFile 'Datestamp the controlFile<\/p>\n
fsObject.CreateFolder fcFileCreator.FullPath 'Create a folder to hold the output files<\/p>\n
'If the overwrite option is avialable wrap this in a if exists check<\/p>\n
For Each ipList In ipControlFile.ListRequests<\/p>\n
'Write the list counts to the controlFile 'Moved to other loop to give realtime feedback<\/p>\n
'ipControlFile.WriteCounts ipList.Pubs, ipList.lType, ipList.SubType, ipList.RecordCount<\/p>\n
If Right(ipList.Pubs,2) <> “–” Then 'Active lists requests<\/p>\n
'Create the file name using the strText variable<\/p>\n
strText = Trim(ipList.Pubs) & \" \" & ipList.lType & \" \" & ipList.SubType & “.xlsx”<\/p>\n
'Write the list object’s data to a new .csv file<\/p>\n
fcFileCreator.CreateFileFromData fsObject, strText , ipList.Data<\/p>\n
End If<\/p>\n
Next<\/p>\n
Set ipList = Nothing 'Release the ipList object, if not already done<\/p>\n
ipControlFile.Maximize 'Maximize the control file to review counts<\/p>\n
MsgBox “Script execution complete. Be sure to save the counts to the control file.”,64,“WhatCounts - ControlFile”<\/p>\n
ExitScript()<\/p>\n
'Exit procedure, exit the script and perform any clean-up if not already done so<\/p>\n
Sub ExitScript<\/p>\n
MsgBox “Exiting Script…” &vbtab &vbtab,“WhatCounts Monthly Update”<\/p>\n
Set fsObject = Nothing<\/p>\n
Set ipControlFile = Nothing<\/p>\n
Set fcFileCreator = Nothing<\/p>\n
Set dbAdvPRD = Nothing<\/p>\n
Set qbQuery = Nothing<\/p>\n
WScript.Quit<\/p>\n
End Sub<\/p>\n
’
\n’
\n’
\n’<\/p>\n
Class cIpostControlFile 'Ipost Specific Control File Class<\/p>\n
Private m_AppObject 'as application object, writeonly<\/p>\n
Private m_directory 'as string<\/p>\n
Private m_file 'as string<\/p>\n
Private m_fullpath 'as string, readonly<\/p>\n
Private m_listRequest 'as ipList object arraylist, readonly<\/p>\n
Public Property Set AppObject(objValue)<\/p>\n
Set m_AppObject = objValue<\/p>\n
End Property<\/p>\n
Public Property Get Directory<\/p>\n
Directory = m_directory<\/p>\n
End Property<\/p>\n
Public Property Let Directory(strValue)<\/p>\n
m_directory = strValue<\/p>\n
End Property<\/p>\n
Public Property Get ControlFile<\/p>\n
ControlFile = m_file<\/p>\n
End Property<\/p>\n
Public Property Let ControlFile(strValue)<\/p>\n
m_file = strValue<\/p>\n
End Property<\/p>\n
Public Property Get FullPath<\/p>\n
FullPath = m_directory & \"\" & m_file<\/p>\n
End Property<\/p>\n
Public Property Get ListRequests<\/p>\n
Set ListRequests = m_listRequest<\/p>\n
End Property<\/p>\n
Public Sub Class_Initialize<\/p>\n
End Sub<\/p>\n
Public Sub Class_Terminate<\/p>\n
Set m_AppObject = Nothing<\/p>\n
Set m_listRequest = Nothing<\/p>\n
End Sub<\/p>\n
Public Sub Maximize<\/p>\n
m_AppObject.windowState = -4143<\/p>\n
End Sub<\/p>\n
Public Sub Minimize<\/p>\n
m_AppObject.windowState = -4140<\/p>\n
End Sub<\/p>\n
Public Sub Open<\/p>\n
'Create an object to hold the controller file<\/p>\n
Dim objExcel<\/p>\n
Set objExcel = m_AppObject<\/p>\n
'Minimize the application window until the script is complete<\/p>\n
objExcel.windowState = -4140<\/p>\n
'Open the control file and organize the file contents<\/p>\n
objExcel.WorkBooks.Open(FullPath)<\/p>\n
objExcel.Visible = True<\/p>\n
'Begin Processing the control file<\/p>\n
ProcessFile(objExcel)<\/p>\n
'Release the reference object<\/p>\n
Set objExcel = Nothing<\/p>\n
End Sub<\/p>\n
Private Sub ProcessFile(objValue)<\/p>\n
Dim objExcel, intLastRow, intLastColumn, objRow, arrList<\/p>\n
Set objExcel = objValue<\/p>\n
Set arrList = CreateObject(“System.Collections.ArrayList”)<\/p>\n
'Create the named ranges to hold the list details and dateheaders<\/p>\n
intLastRow = objExcel.WorkSheets(1).UsedRange.Rows.Count<\/p>\n
intLastColumn = objExcel.WorkSheets(1).UsedRange.Columns.Count + 1<\/p>\n
objExcel.Range(“A6”, “C” & intLastRow).Name = “RequestedLists”<\/p>\n
objExcel.Range(“E4”, objExcel.Cells(4, objExcel.Rows(4).Cells.Count)).Name = “DateRanges”<\/p>\n
objExcel.Range(objExcel.Cells(6, intLastColumn), objExcel.Cells(intLastRow,intLastColumn)).Name = “CurrReport”<\/p>\n
'Format the named ranges<\/p>\n
objExcel.Range(“DateRanges”).Font.Bold = True<\/p>\n
objExcel.Range(“DateRanges”).NumberFormat = “mm/yyyy”<\/p>\n
objExcel.Range(“CurrReport”).NumberFormat = “#,##0<\/span>”<\/p>\n 'Loop through each list request in the ‘requestedlists’ range<\/p>\n For Each objRow In objExcel.Range(“RequestedLists”).Rows<\/p>\n If Not IsEmpty(objRow.Cells(1).Value) Then<\/p>\n arrList.Add objRow.Cells(1).Value<\/p>\n arrList.Add objRow.Cells(2).Value<\/p>\n arrList.add objRow.Cells(3).Value<\/p>\n End If<\/p>\n Next<\/p>\n 'Return the array list<\/p>\n Set m_listRequest = AsIpObject(arrList)<\/p>\n 'Clean-up objects<\/p>\n Set objExcel = Nothing<\/p>\n Set arrList = Nothing<\/p>\n End Sub<\/p>\n Private Function AsIpObject(arrValue)<\/p>\n Dim arrTemp, ipTempObject, arrIpObjectList, i<\/p>\n Set arrTemp = arrValue<\/p>\n Set arrIpObjectList = CreateObject(“System.Collections.ArrayList”)<\/p>\n For i = 0 To arrTemp.Count - 1<\/p>\n Set ipTempObject = New cIpostList<\/p>\n ipTempObject.Pubs = arrTemp(i)<\/p>\n ipTempObject.lType = arrTemp(i + 1)<\/p>\n ipTempObject.SubType = arrTemp(i + 2)<\/p>\n arrIpObjectList.Add ipTempObject<\/p>\n i = i + 2<\/p>\n Next<\/p>\n 'Return the new list of ipList objects<\/p>\n Set AsIpObject = arrIpObjectList<\/p>\n 'Clean-Up objects<\/p>\n Set arrIpObjectList = Nothing<\/p>\n End Function<\/p>\n Public Sub WriteCounts(strPubs,strType,strSubType,intCount)<\/p>\n Dim objExcel<\/p>\n Set objExcel = m_AppObject<\/p>\n Dim objRow, i<\/p>\n i = 1<\/p>\n For Each objRow In objExcel.Range(“RequestedLists”).Rows<\/p>\n If Not IsEmpty(objRow.Cells(1).Value) Then<\/p>\n If strPubs = objRow.Cells(1).Value And strType = objRow.Cells(2).Value And strSubType = objRow.Cells(3).Value Then<\/p>\n objExcel.Range(“CurrReport”).Cells(i).Value = intCount<\/p>\n 'If the row is highlighted then continue on the new cell<\/p>\n If objExcel.Range(“CurrReport”).Cells(i).OffSet(0,-1).Interior.ColorIndex = 15 Then<\/p>\n objExcel.Range(“CurrReport”).Cells(i).Interior.ColorIndex = 15<\/p>\n End If<\/p>\n End If i = i + 1<\/p>\n Next<\/p>\n Set objExcel = Nothing<\/p>\n End Sub<\/p>\n Public Sub DateStampFile<\/p>\n Dim objExcel, intLastColumn<\/p>\n Set objExcel = m_AppObject<\/p>\n 'Find the used Column<\/p>\n intLastColumn = objExcel.WorkSheets(1).UsedRange.Columns.Count<\/p>\n 'Find the first empty cell and insert the current date<\/p>\n objExcel.Range(“DateRanges”).Cells(intLastColumn - 4).Value = Date<\/p>\n Set objExcel = Nothing<\/p>\n End Sub<\/p>\n End Class<\/p>\n Class cIpostList 'Ipost List Class<\/p>\n Private m_pubs 'as string<\/p>\n Private m_type 'as string<\/p>\n Private m_subType 'as String<\/p>\n Private m_count 'as integer<\/p>\n Private m_data 'as recordset<\/p>\n Public Property Get Pubs<\/p>\n Pubs = m_pubs<\/p>\n End Property<\/p>\n Public Property Let Pubs(strValue)<\/p>\n m_pubs = strValue<\/p>\n End Property<\/p>\n Public Property Get lType<\/p>\n lType = m_type<\/p>\n End Property<\/p>\n Public Property Let lType(strValue)<\/p>\n m_type = strValue<\/p>\n End Property<\/p>\n Public Property Get SubType<\/p>\n SubType = m_subType<\/p>\n End Property<\/p>\n Public Property Let SubType(strValue)<\/p>\n m_subType = strValue<\/p>\n End Property<\/p>\n Public Property Get RecordCount<\/p>\n RecordCount = m_count<\/p>\n End Property<\/p>\n Public Property Let RecordCount(intValue)<\/p>\n m_count = intValue<\/p>\n End Property<\/p>\n Public Property Get Data<\/p>\n Set Data = m_data<\/p>\n End Property<\/p>\n Public Property Set Data(rsValue)<\/p>\n Set m_data = rsValue<\/p>\n End Property<\/p>\n Public Sub Class_Initialize<\/p>\n 'Set intial record count to 0<\/p>\n m_count = 0<\/p>\n End Sub<\/p>\n Public Sub Class_Terminate<\/p>\n 'Release data recordset<\/p>\n Set m_data = Nothing<\/p>\n End Sub<\/p>\n End Class<\/p>\n Class cQueryBuilder 'Query Builder Class<\/p>\n Private m_sql 'as string, readonly<\/p>\n Public Property Get SqlText<\/p>\n SqlText = m_sql<\/p>\n End Property<\/p>\n Public Sub Class_Initialize<\/p>\n End Sub<\/p>\n Public sub Class_Terminate<\/p>\n End Sub<\/p>\n Public Sub ProcessRequest(ipList)<\/p>\n 'Empty sql string<\/p>\n m_sql = “”<\/p>\n Select Case UCase(ipList.lType)<\/p>\n Case “SUBSCRIPTIONS”<\/p>\n BuildSubscriptionQuery ipList<\/p>\n Case “PRODUCTS”<\/p>\n BuildCustomerQuery ipList<\/p>\n Case Else<\/p>\n 'UI.Message unknow list type End Select<\/p>\n End Sub<\/p>\n Private Sub BuildSubscriptionQuery(ipList)<\/p>\n Dim strPubs, strStatus<\/p>\n strPubs = “'” & Replace(Trim(ipList.Pubs),\" “,”,\") & “'”<\/p>\n strStatus = “‘w,r,p,e’”<\/p>\n m_sql = \"IF OBJECT_ID(‘AdvDbPRD.Ipost.GetSubscriptions’,‘P’) IS NOT NULL \" & _ End Sub<\/p>\n Private Sub BuildCustomerQuery(ipList)<\/p>\n Dim arrPubs, strPubs, i<\/p>\n arrPubs = Split(ipList.Pubs)<\/p>\n strPubs = “”<\/p>\n i = 0<\/p>\n For i = 0 to UBound(arrPubs)<\/p>\n Select Case arrPubs(i)<\/p>\n Case “CAP” Case “FCM” Case “GEM” Case “STM” Case “IMA” Case “GRT” Case “HBC” Case “MEL” Case “HFH” Case “MCC” Case “NHG” Case “UTR” Case “MEN” Case “CFR” Case “HGR” Case “FER” Case “CCK” End Select<\/p>\n Next<\/p>\n strPubs = “'” & Replace(Trim(strPubs),\" “,”,\") & “'”<\/p>\n m_sql = \"IF OBJECT_ID(‘AdvDbPRD.Ipost.GetProducts’,‘P’) IS NOT NULL \" & _ End Sub<\/p>\n End Class<\/p>\n Class cFileCreator 'File Creator Class<\/p>\n Private m_location 'as string<\/p>\n Private m_folder 'as string<\/p>\n Private m_fullpath 'as string, readonly<\/p>\n Public Property Get Location<\/p>\n Location = m_location<\/p>\n End Property<\/p>\n Public Property Let Location(strValue)<\/p>\n m_location = strValue<\/p>\n End Property<\/p>\n Public Property Get Folder<\/p>\n Folder = m_folder<\/p>\n End Property<\/p>\n Public Property Let Folder(strValue)<\/p>\n m_folder = strValue<\/p>\n End Property<\/p>\n Public Property Get FullPath<\/p>\n FullPath = m_location & \"\" & m_folder<\/p>\n End Property<\/p>\n Public Sub CreateFileFromData(fsObject,strFileName,rsData)<\/p>\n Dim oExcel With oSheet oSheet.Range(“A2”).CopyFromRecordset rsData<\/p>\n oBook.SaveAs FullPath & \"\" & strFileName<\/p>\n oExcel.Quit<\/p>\n Set rsData = Nothing<\/p>\n End Sub<\/p>\n End Class<\/p>\n Class cDataBase 'Database Control Class<\/p>\n Private m_adoConn 'as ado connection, private<\/p>\n Private m_strConn 'as string, private<\/p>\n Private m_connStatus 'as string, readonly<\/p>\n Public Property Get Status<\/p>\n Status = m_connStatus<\/p>\n End Property<\/p>\n Public Sub Class_Initialize<\/p>\n Set m_adoConn = CreateObject(“ADODB.Connection”)<\/p>\n 'This line is needed to provide the recordcount, change cursor to static.<\/p>\n m_adoConn.CursorLocation = 3<\/p>\n 'This line is needed to tell ado to wait indefinitely for the results.<\/p>\n m_adoConn.CommandTimeOut = 0<\/p>\n 'Provide the connection string.<\/p>\n m_strConn = “PROVIDER=sqloledb;”<\/p>\n m_strConn = m_strConn & “DATA SOURCE=OPIDB1;”<\/p>\n m_strConn = m_strConn & “INITIAL CATALOG=AdvDbPRD;”<\/p>\n m_strConn = m_strConn & “USER ID=sa;”<\/p>\n m_strConn = m_strConn & “Password=Opis;”<\/p>\n 'Set connection status.<\/p>\n m_connStatus = m_adoConn.State<\/p>\n End Sub<\/p>\n Private Sub OpenConnection<\/p>\n m_adoConn.Open m_strConn<\/p>\n 'Set the connection status.<\/p>\n m_connStatus = m_adoConn.State<\/p>\n End sub<\/p>\n Private Sub CloseConnection<\/p>\n m_adoConn.Close<\/p>\n 'Set the connection status.<\/p>\n m_connStatus = m_adoConn.State<\/p>\n End Sub<\/p>\n Public Function ExecuteQuery(strSql)<\/p>\n OpenConnection()<\/p>\n Dim rsResults<\/p>\n Set rsResults = m_adoConn.Execute(strSql)<\/p>\n 'Test for empty recordset<\/p>\n If Not rsResults.EOF And Not rsResults.BOF Then<\/p>\n rsResults.MoveFirst<\/p>\n End If<\/p>\n 'Disconnect the record set from the active connection.<\/p>\n Set rsResults.ActiveConnection = Nothing<\/p>\n 'Return the results as a disconnected recordset.<\/p>\n Set ExecuteQuery = rsResults<\/p>\n Set rsResults = Nothing<\/p>\n CloseConnection()<\/p>\n End Function<\/p>\n Public Sub Class_Terminate<\/p>\n Set m_adoConn = Nothing<\/p>\n m_strConn = “”<\/p>\n m_connStatus = “”<\/p>\n End Sub<\/p>\n Public Sub Clean<\/p>\n Set m_adoConn = Nothing<\/p>\n m_strConn = “”<\/p>\n m_connStatus = “”<\/p>\n End Sub<\/p>\n End Class<\/p>","upvoteCount":4,"answerCount":5,"datePublished":"2020-08-26T18:31:12.000Z","author":{"@type":"Person","name":"tramirez8","url":"https://community.spiceworks.com/u/tramirez8"},"suggestedAnswer":[{"@type":"Answer","text":" I know this likely sounds simple but this was just dumped on me after an employee left. The current script writes an Excel file and they now want it to be a .csv. I am new to VBS and need some pointers. It would be nice if I could just change the file extension but know this will not give me the proper formatting. This is the current script:<\/p>\n Option Explicit<\/p>\n Dim fsObject, ipControlFile, fcFileCreator, dbAdvPRD, qbQuery<\/p>\n Dim ipList, rsListResults, strText, intResult<\/p>\n Set fsObject = CreateObject(“Scripting.FileSystemObject”)<\/p>\n Set ipControlFile = New cIpostControlFile<\/p>\n Set fcFileCreator = New cFileCreator<\/p>\n Set dbAdvPRD = New cDataBase<\/p>\n Set qbQuery = New cQueryBuilder<\/p>\n Set ipControlFile.AppObject = CreateObject(“Excel.Application”)<\/p>\n ipControlFile.Directory = fsObject.GetAbsolutePathName(“.”)<\/p>\n ipControlFile.ControlFile = “WhatCounts List Counts.xls”<\/p>\n fcFileCreator.Location = fsObject.GetAbsolutePathName(“.”)<\/p>\n fcFileCreator.Folder = \"WhatCounts Out \" & DatePart(“m”,Now) & “-” & DatePart(“yyyy”,Now)<\/p>\n 'Begin Script<\/p>\n MsgBox “Retrieving data. This may take several minutes.”, 64, “Monthly WhatCounts Update”<\/p>\n If Not fsObject.FileExists(ipControlFile.FullPath) Then<\/p>\n strText = “The \" & ipControlFile.ControlFile & \" file is not available.”<\/p>\n strText = strText & “The file must be located in the same directory as this script.”<\/p>\n MsgBox strText, 16, “Error - ControlFile Not Found”<\/p>\n ExitScript()<\/p>\n End If<\/p>\n If fsObject.FolderExists(fcFileCreator.FullPath) Then<\/p>\n strText = \"A folder with this datestamp already exists \"<\/p>\n strText = strText & \"and contains \"<\/p>\n strText = strText & fsObject.GetFolder(fcFileCreator.FullPath).Files.Count & \" file(s). \"<\/p>\n strText = strText & \" Would you like to use a this folder?\"<\/p>\n intResult = MsgBox(strText, 35, “WhatCounts Monthly Update - Ouput directory”)<\/p>\n If intResult = 2 Then 'User selects cancel, exit script<\/p>\n ExitScript()<\/p>\n End If<\/p>\n If intResult = 7 Then 'User selects no, use temp folder<\/p>\n strText = \"WhatCounts Out Temp \" & DatePart(“m”,Now) & “-” & DatePart(“yyyy”,Now)<\/p>\n strText = strText & \" \" & DatePart(“h”,now) & Datepart(“n”, Now) & DatePart(“s”, Now)<\/p>\n fcFileCreator.Folder = strText<\/p>\n End If<\/p>\n If intResult = 6 Then 'User selects yes, use default folder<\/p>\n strText = “The file(s) in this folder will be overwritten.”<\/p>\n intResult = MsgBox (strText,49,“WhatCounts Monthly Update - Output directory”)<\/p>\n If intResult = 2 Then 'User cancelled afterall<\/p>\n ExitScript()<\/p>\n End If<\/p>\n MsgBox “This options is currently not supported.”,16,“Whatcounts Monthly Update - Ouput directory”<\/p>\n ExitScript()<\/p>\n End If<\/p>\n End If<\/p>\n ipControlFile.Open 'The controlFile exists and should be opened for processing<\/p>\n 'Once the file has been processed we can return the request objects<\/p>\n For Each ipList In ipControlFile.ListRequests<\/p>\n If Right(ipList.Pubs,2) <> “–” Then 'Active list requests, not commented out in the controlfile<\/p>\n qbQuery.ProcessRequest ipList<\/p>\n Set rsListResults = dbAdvPRD.ExecuteQuery(qbQuery.SqlText)<\/p>\n ipList.RecordCount = rsListResults.RecordCount<\/p>\n Set ipList.Data = rsListResults 'Add the results to the ipList object’s data property<\/p>\n Set rsListResults = Nothing 'Release the temporary recordset<\/p>\n Else 'Set the inactive request’s count to NULL<\/p>\n ipList.RecordCount = NULL<\/p>\n End If<\/p>\n ipControlFile.WriteCounts ipList.Pubs, ipList.lType, ipList.SubType, ipList.RecordCount 'Write the counts<\/p>\n Next<\/p>\n ipControlFile.DateStampFile 'Datestamp the controlFile<\/p>\n fsObject.CreateFolder fcFileCreator.FullPath 'Create a folder to hold the output files<\/p>\n 'If the overwrite option is avialable wrap this in a if exists check<\/p>\n For Each ipList In ipControlFile.ListRequests<\/p>\n 'Write the list counts to the controlFile 'Moved to other loop to give realtime feedback<\/p>\n 'ipControlFile.WriteCounts ipList.Pubs, ipList.lType, ipList.SubType, ipList.RecordCount<\/p>\n If Right(ipList.Pubs,2) <> “–” Then 'Active lists requests<\/p>\n 'Create the file name using the strText variable<\/p>\n strText = Trim(ipList.Pubs) & \" \" & ipList.lType & \" \" & ipList.SubType & “.xlsx”<\/p>\n 'Write the list object’s data to a new .csv file<\/p>\n fcFileCreator.CreateFileFromData fsObject, strText , ipList.Data<\/p>\n End If<\/p>\n Next<\/p>\n Set ipList = Nothing 'Release the ipList object, if not already done<\/p>\n ipControlFile.Maximize 'Maximize the control file to review counts<\/p>\n MsgBox “Script execution complete. Be sure to save the counts to the control file.”,64,“WhatCounts - ControlFile”<\/p>\n ExitScript()<\/p>\n 'Exit procedure, exit the script and perform any clean-up if not already done so<\/p>\n Sub ExitScript<\/p>\n MsgBox “Exiting Script…” &vbtab &vbtab,“WhatCounts Monthly Update”<\/p>\n Set fsObject = Nothing<\/p>\n Set ipControlFile = Nothing<\/p>\n Set fcFileCreator = Nothing<\/p>\n Set dbAdvPRD = Nothing<\/p>\n Set qbQuery = Nothing<\/p>\n WScript.Quit<\/p>\n End Sub<\/p>\n ’ Class cIpostControlFile 'Ipost Specific Control File Class<\/p>\n Private m_AppObject 'as application object, writeonly<\/p>\n Private m_directory 'as string<\/p>\n Private m_file 'as string<\/p>\n Private m_fullpath 'as string, readonly<\/p>\n Private m_listRequest 'as ipList object arraylist, readonly<\/p>\n Public Property Set AppObject(objValue)<\/p>\n Set m_AppObject = objValue<\/p>\n End Property<\/p>\n Public Property Get Directory<\/p>\n Directory = m_directory<\/p>\n End Property<\/p>\n Public Property Let Directory(strValue)<\/p>\n m_directory = strValue<\/p>\n End Property<\/p>\n Public Property Get ControlFile<\/p>\n ControlFile = m_file<\/p>\n End Property<\/p>\n Public Property Let ControlFile(strValue)<\/p>\n m_file = strValue<\/p>\n End Property<\/p>\n Public Property Get FullPath<\/p>\n FullPath = m_directory & \"\" & m_file<\/p>\n End Property<\/p>\n Public Property Get ListRequests<\/p>\n Set ListRequests = m_listRequest<\/p>\n End Property<\/p>\n Public Sub Class_Initialize<\/p>\n End Sub<\/p>\n Public Sub Class_Terminate<\/p>\n Set m_AppObject = Nothing<\/p>\n Set m_listRequest = Nothing<\/p>\n End Sub<\/p>\n Public Sub Maximize<\/p>\n m_AppObject.windowState = -4143<\/p>\n End Sub<\/p>\n Public Sub Minimize<\/p>\n m_AppObject.windowState = -4140<\/p>\n End Sub<\/p>\n Public Sub Open<\/p>\n 'Create an object to hold the controller file<\/p>\n Dim objExcel<\/p>\n Set objExcel = m_AppObject<\/p>\n 'Minimize the application window until the script is complete<\/p>\n objExcel.windowState = -4140<\/p>\n 'Open the control file and organize the file contents<\/p>\n objExcel.WorkBooks.Open(FullPath)<\/p>\n objExcel.Visible = True<\/p>\n 'Begin Processing the control file<\/p>\n ProcessFile(objExcel)<\/p>\n 'Release the reference object<\/p>\n Set objExcel = Nothing<\/p>\n End Sub<\/p>\n Private Sub ProcessFile(objValue)<\/p>\n Dim objExcel, intLastRow, intLastColumn, objRow, arrList<\/p>\n Set objExcel = objValue<\/p>\n Set arrList = CreateObject(“System.Collections.ArrayList”)<\/p>\n 'Create the named ranges to hold the list details and dateheaders<\/p>\n intLastRow = objExcel.WorkSheets(1).UsedRange.Rows.Count<\/p>\n intLastColumn = objExcel.WorkSheets(1).UsedRange.Columns.Count + 1<\/p>\n objExcel.Range(“A6”, “C” & intLastRow).Name = “RequestedLists”<\/p>\n objExcel.Range(“E4”, objExcel.Cells(4, objExcel.Rows(4).Cells.Count)).Name = “DateRanges”<\/p>\n objExcel.Range(objExcel.Cells(6, intLastColumn), objExcel.Cells(intLastRow,intLastColumn)).Name = “CurrReport”<\/p>\n 'Format the named ranges<\/p>\n objExcel.Range(“DateRanges”).Font.Bold = True<\/p>\n objExcel.Range(“DateRanges”).NumberFormat = “mm/yyyy”<\/p>\n objExcel.Range(“CurrReport”).NumberFormat = “#,##0<\/span>”<\/p>\n 'Loop through each list request in the ‘requestedlists’ range<\/p>\n For Each objRow In objExcel.Range(“RequestedLists”).Rows<\/p>\n If Not IsEmpty(objRow.Cells(1).Value) Then<\/p>\n arrList.Add objRow.Cells(1).Value<\/p>\n arrList.Add objRow.Cells(2).Value<\/p>\n arrList.add objRow.Cells(3).Value<\/p>\n End If<\/p>\n Next<\/p>\n 'Return the array list<\/p>\n Set m_listRequest = AsIpObject(arrList)<\/p>\n 'Clean-up objects<\/p>\n Set objExcel = Nothing<\/p>\n Set arrList = Nothing<\/p>\n End Sub<\/p>\n Private Function AsIpObject(arrValue)<\/p>\n Dim arrTemp, ipTempObject, arrIpObjectList, i<\/p>\n Set arrTemp = arrValue<\/p>\n Set arrIpObjectList = CreateObject(“System.Collections.ArrayList”)<\/p>\n For i = 0 To arrTemp.Count - 1<\/p>\n Set ipTempObject = New cIpostList<\/p>\n ipTempObject.Pubs = arrTemp(i)<\/p>\n ipTempObject.lType = arrTemp(i + 1)<\/p>\n ipTempObject.SubType = arrTemp(i + 2)<\/p>\n arrIpObjectList.Add ipTempObject<\/p>\n i = i + 2<\/p>\n Next<\/p>\n 'Return the new list of ipList objects<\/p>\n Set AsIpObject = arrIpObjectList<\/p>\n 'Clean-Up objects<\/p>\n Set arrIpObjectList = Nothing<\/p>\n End Function<\/p>\n Public Sub WriteCounts(strPubs,strType,strSubType,intCount)<\/p>\n Dim objExcel<\/p>\n Set objExcel = m_AppObject<\/p>\n Dim objRow, i<\/p>\n i = 1<\/p>\n For Each objRow In objExcel.Range(“RequestedLists”).Rows<\/p>\n If Not IsEmpty(objRow.Cells(1).Value) Then<\/p>\n If strPubs = objRow.Cells(1).Value And strType = objRow.Cells(2).Value And strSubType = objRow.Cells(3).Value Then<\/p>\n objExcel.Range(“CurrReport”).Cells(i).Value = intCount<\/p>\n 'If the row is highlighted then continue on the new cell<\/p>\n If objExcel.Range(“CurrReport”).Cells(i).OffSet(0,-1).Interior.ColorIndex = 15 Then<\/p>\n objExcel.Range(“CurrReport”).Cells(i).Interior.ColorIndex = 15<\/p>\n End If<\/p>\n End If i = i + 1<\/p>\n Next<\/p>\n Set objExcel = Nothing<\/p>\n End Sub<\/p>\n Public Sub DateStampFile<\/p>\n Dim objExcel, intLastColumn<\/p>\n Set objExcel = m_AppObject<\/p>\n 'Find the used Column<\/p>\n intLastColumn = objExcel.WorkSheets(1).UsedRange.Columns.Count<\/p>\n 'Find the first empty cell and insert the current date<\/p>\n objExcel.Range(“DateRanges”).Cells(intLastColumn - 4).Value = Date<\/p>\n Set objExcel = Nothing<\/p>\n End Sub<\/p>\n End Class<\/p>\n Class cIpostList 'Ipost List Class<\/p>\n Private m_pubs 'as string<\/p>\n Private m_type 'as string<\/p>\n Private m_subType 'as String<\/p>\n Private m_count 'as integer<\/p>\n Private m_data 'as recordset<\/p>\n Public Property Get Pubs<\/p>\n Pubs = m_pubs<\/p>\n End Property<\/p>\n Public Property Let Pubs(strValue)<\/p>\n m_pubs = strValue<\/p>\n End Property<\/p>\n Public Property Get lType<\/p>\n lType = m_type<\/p>\n End Property<\/p>\n Public Property Let lType(strValue)<\/p>\n m_type = strValue<\/p>\n End Property<\/p>\n Public Property Get SubType<\/p>\n SubType = m_subType<\/p>\n End Property<\/p>\n Public Property Let SubType(strValue)<\/p>\n m_subType = strValue<\/p>\n End Property<\/p>\n Public Property Get RecordCount<\/p>\n RecordCount = m_count<\/p>\n End Property<\/p>\n Public Property Let RecordCount(intValue)<\/p>\n m_count = intValue<\/p>\n End Property<\/p>\n Public Property Get Data<\/p>\n Set Data = m_data<\/p>\n End Property<\/p>\n Public Property Set Data(rsValue)<\/p>\n Set m_data = rsValue<\/p>\n End Property<\/p>\n Public Sub Class_Initialize<\/p>\n 'Set intial record count to 0<\/p>\n m_count = 0<\/p>\n End Sub<\/p>\n Public Sub Class_Terminate<\/p>\n 'Release data recordset<\/p>\n Set m_data = Nothing<\/p>\n End Sub<\/p>\n End Class<\/p>\n Class cQueryBuilder 'Query Builder Class<\/p>\n Private m_sql 'as string, readonly<\/p>\n Public Property Get SqlText<\/p>\n SqlText = m_sql<\/p>\n End Property<\/p>\n Public Sub Class_Initialize<\/p>\n End Sub<\/p>\n Public sub Class_Terminate<\/p>\n End Sub<\/p>\n Public Sub ProcessRequest(ipList)<\/p>\n 'Empty sql string<\/p>\n m_sql = “”<\/p>\n Select Case UCase(ipList.lType)<\/p>\n Case “SUBSCRIPTIONS”<\/p>\n BuildSubscriptionQuery ipList<\/p>\n Case “PRODUCTS”<\/p>\n BuildCustomerQuery ipList<\/p>\n Case Else<\/p>\n 'UI.Message unknow list type End Select<\/p>\n End Sub<\/p>\n Private Sub BuildSubscriptionQuery(ipList)<\/p>\n Dim strPubs, strStatus<\/p>\n strPubs = “'” & Replace(Trim(ipList.Pubs),\" “,”,\") & “'”<\/p>\n strStatus = “‘w,r,p,e’”<\/p>\n m_sql = \"IF OBJECT_ID(‘AdvDbPRD.Ipost.GetSubscriptions’,‘P’) IS NOT NULL \" & _ End Sub<\/p>\n Private Sub BuildCustomerQuery(ipList)<\/p>\n Dim arrPubs, strPubs, i<\/p>\n arrPubs = Split(ipList.Pubs)<\/p>\n strPubs = “”<\/p>\n i = 0<\/p>\n For i = 0 to UBound(arrPubs)<\/p>\n Select Case arrPubs(i)<\/p>\n Case “CAP” Case “FCM” Case “GEM” Case “STM” Case “IMA” Case “GRT” Case “HBC” Case “MEL” Case “HFH” Case “MCC” Case “NHG” Case “UTR” Case “MEN” Case “CFR” Case “HGR” Case “FER” Case “CCK” End Select<\/p>\n Next<\/p>\n strPubs = “'” & Replace(Trim(strPubs),\" “,”,\") & “'”<\/p>\n
\nEnd If<\/p>\n
\nmsgbox(“unknown list type”)<\/p>\n
\n\"EXEC Ipost.GetSubscriptions @PUBS<\/a> = \" & strPubs & \", \" & _
\n\"@STATUS<\/a> = \" & strStatus<\/p>\n
\nstrPubs = strPubs & “cp” & \" \"<\/p>\n
\nstrPubs = strPubs & “fc” & \" \"<\/p>\n
\nstrPubs = strPubs & “ge” & \" \"<\/p>\n
\nstrPubs = strPubs & “ia” & \" \"<\/p>\n
\nstrPubs = strPUbs & “ia” & \" \"<\/p>\n
\nstrPubs = strPubs & “gr” & \" \"<\/p>\n
\nstrPubs = strPubs & “hc” & \" \"<\/p>\n
\nstrPubs = strPUbs & “ml” & \" \"<\/p>\n
\nstrPubs = strPUbs & “hf” & \" \"<\/p>\n
\nstrPubs = strPubs & “mc” & \" \"<\/p>\n
\nstrPubs = strPubs & “nh” & \" \"<\/p>\n
\nstrPubs = strPubs & “ut” & \" \"<\/p>\n
\nstrPubs = strPubs & “me” & \" \"<\/p>\n
\nstrPubs = strPubs & “cf” & \" \"<\/p>\n
\nstrPubs = strPubs & “hg” & \" \"<\/p>\n
\nstrPubs = strPubs & “fr” & \" \"<\/p>\n
\nstrPubs = strPubs & “cc” & \" \"<\/p>\n
\n\"EXEC Ipost.GetProducts @PUBS<\/a> = \" & strPubs<\/p>\n
\nDim oBook
\nDim oSheet
\nDim fld
\nDim i
\ni = 1
\nSet oExcel = CreateObject(“Excel.Application”)
\nSet oBook = oExcel.Workbooks.Add
\nSet oSheet = oBook.Worksheets(1)<\/p>\n
\nFor Each fld in rsData.Fields
\n.Cells(1,i).Value = fld.Name
\ni = i + 1
\nNext
\nEnd With<\/p>\n
\n’
\n’
\n’<\/p>\n
\nEnd If<\/p>\n
\nmsgbox(“unknown list type”)<\/p>\n
\n\"EXEC Ipost.GetSubscriptions @PUBS<\/a> = \" & strPubs & \", \" & _
\n\"@STATUS<\/a> = \" & strStatus<\/p>\n
\nstrPubs = strPubs & “cp” & \" \"<\/p>\n
\nstrPubs = strPubs & “fc” & \" \"<\/p>\n
\nstrPubs = strPubs & “ge” & \" \"<\/p>\n
\nstrPubs = strPubs & “ia” & \" \"<\/p>\n
\nstrPubs = strPUbs & “ia” & \" \"<\/p>\n
\nstrPubs = strPubs & “gr” & \" \"<\/p>\n
\nstrPubs = strPubs & “hc” & \" \"<\/p>\n
\nstrPubs = strPUbs & “ml” & \" \"<\/p>\n
\nstrPubs = strPUbs & “hf” & \" \"<\/p>\n
\nstrPubs = strPubs & “mc” & \" \"<\/p>\n
\nstrPubs = strPubs & “nh” & \" \"<\/p>\n
\nstrPubs = strPubs & “ut” & \" \"<\/p>\n
\nstrPubs = strPubs & “me” & \" \"<\/p>\n
\nstrPubs = strPubs & “cf” & \" \"<\/p>\n
\nstrPubs = strPubs & “hg” & \" \"<\/p>\n
\nstrPubs = strPubs & “fr” & \" \"<\/p>\n
\nstrPubs = strPubs & “cc” & \" \"<\/p>\n