Avatar of mkraemer11
mkraemer11
Flag for United States of America asked on

VBS script - Need to change location of output file

The following script places the outputfile on the desktop.  I need it  to be placed in "C:\FaxConfirmation".  Can anyone tell me what I need to add in the coding and where?

' Specify the template xls file that you wish to update here
strTemplateFile = Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "FaxConfirmation.xls"


Set objExcel = CreateObject("Excel.Application")
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.CommonDialog")
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).UsedRange.Copy objTemplateWB.Sheets(1).Cells(65536, "A").End(xlUp).Offset(1, 0)
      objCSVWB.Activate
      objExcel.ActiveWorkbook.Close False
      objTemplateWB.Activate
      objExcel.ActiveWorkbook.SaveAs objExcel.ActiveWorkbook.Path & "\" & Replace(objExcel.ActiveWorkbook.Name, ".xls", "") & "_" & FormatDateToString(Now) & ".xls"
      objExcel.ActiveWorkbook.Close False
      objExcel.Quit
End If

Function FormatDateToString(dDateTime)
      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
VB Script

Avatar of undefined
Last Comment
omgang

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
omgang

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
mkraemer11

ASKER
made recommended changes but now script has an error at line 12
 

' Specify the template xls file that you wish to update here
strTemplateFile = Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "FaxConfirmation.xls"

Set objExcel = CreateObject("Excel.Application")
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.CommonDialog")
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).UsedRange.Copy objTemplateWB.Sheets(1).Cells(65536, "A").End(xlUp).Offset(1, 0)
      objCSVWB.Activate
      objExcel.ActiveWorkbook.Close False
      objTemplateWB.Activate
      objExcel.ActiveWorkbook.SaveAs "C:\FaxConfirmation\" & Replace(objExcel.ActiveWorkbook.Name, ".xls", "") & "_" & FormatDateToString(Now) & ".xls"
      objExcel.ActiveWorkbook.Close False
      objExcel.Quit
End If
Function FormatDateToString(dDateTime)
      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

 

VBS-error.doc
omgang

The error is telling you the script cannot find the specified file.
Line 12 is objExcel.Workbooks.Open strTemplateFile
strTemplate file is FaxConfirmation.xls located in the same folder where the script itself is located.

You need to have an Excel template file named FaxConfirmation.xls in the same folder as the script.
If you want to do this from the c:\FaxConfirmation folder then simply run the script from that folder.  If so, the original script should do what you want without the earlier change.
OM Gang
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23