Wow, that was fast. I did not expect a reply until Monday. I modified the script to eliminate the dialog box so it could run in a noninteractive mode, but the script works great and solved my problem.
However, if it is not too much trouble, would you walk me through the logic? I follow some of it from what I could gleam from the Scripting Guys column. I am most interested in the constants. (I'm not sure how they define the beginning and end of the spreadsheets.) I am also very intertested in the line:
objCSVWB.Sheets(1).UsedRan
What is 65536?
If you could also point me in the right direction for good VBScript educational resources, I would also appreciate it.
Best Regards and Happy Holidays
Burzin
Main Topics
Browse All Topics





by: RobSampsonPosted on 2007-12-25 at 16:55:27ID: 20528091
Try this VBS file. Paste the code below into a text document, and save it with a VBS extension, not TXT. Then change
Name, WScript.ScriptName, "") & "TestTemplate.xls"
ation")
.CommonDia log")
ge.Copy objTemplateWB.Sheets(1).Ce lls(65536, "A").End(xlUp).Offset(1, 0) ose False veAs objExcel.ActiveWorkbook.Pa th & "\" & Replace(objExcel.ActiveWor kbook.Name , ".xls", "") & "_" & FormatDateToString(Now) & ".xls" ose False
me)
strTemplateFile
to suit your file, and see how you go.
When you run it, it will open the template file, then ask you to find the CSV file, then
add the contents from the CSV file to the end of the template file, then save the template
file as a new name with the date added, then close Excel.
'==============
' Specify the template xls file that you wish to update here
strTemplateFile = Replace(WScript.ScriptFull
Set objExcel = CreateObject("Excel.Applic
Const xlUp = -4162
Const xlDown = -4121
Const xlAnd = 1
objExcel.Visible = True
'First open the template workbook
objExcel.Workbooks.Open strTemplateFile
Set objTemplateWB = objExcel.ActiveWorkbook
Set objDialog = CreateObject("UserAccounts
objDialog.Filter = "CSV Files (*.csv)|*.csv;All Files (*.*)|*.*"
objDialog.FilterIndex = 1
objDialog.InitialDir = "."
intResult = objDialog.ShowOpen
If intResult = 0 Then
MsgBox "No file was selected."
objExcel.Quit
Else
objExcel.Workbooks.Open objDialog.FileName
Set objCSVWB = objExcel.ActiveWorkbook
objCSVWB.Sheets(1).UsedRan
objCSVWB.Activate
objExcel.ActiveWorkbook.Cl
objTemplateWB.Activate
objExcel.ActiveWorkbook.Sa
objExcel.ActiveWorkbook.Cl
objExcel.Quit
End If
Function FormatDateToString(dDateTi
FormatDateToString = Year(dDateTime) & "-" &_
Right("00" & Month(dDateTime), 2) & "-" &_
Right("00" & Day(dDateTime), 2) & "-" &_
Right("00" & Hour(dDateTime), 2) & "-" &_
Right("00" & Minute(dDateTime), 2) & "-" &_
Right("00" & Second(dDateTime), 2)
End Function
'==============
Regards,
Rob.