Link to home
Start Free TrialLog in
Avatar of arundelr
arundelr

asked on

Conditional check in VbScript

Hi,

In this script the input filename is passed as a parameter at runtime  (Thanks to Idle_Mind ;)

Like so...
strOrigFile = WScript.Arguments(0)

And at runtime...
wscript C:\testing\csv_to_xls.vbs C:\testing\in.csv

Now I want do do a conditional statement after this, basically saving if you cannot open the input file then just give up and die gracefully, currently if the file does not exist the script exits with error on line 23
objExcel.Workbooks.Open strOrigFile

Can somebody please help?

Thanks

Rob


Option Explicit
 
 
Dim fldr, f, file,strOrigFile, strFile, fso, strDirectory
Dim objExcel
 
'strOrigFile = "C:\testing\in.csv"
 
'******************************************************
'The input file name is passed at run time i.e. wscript C:\testing\csv_to_xls.vbs C:\testing\in.csv
'******************************************************
 
 
strOrigFile = WScript.Arguments(0)
 
'******************************************************
'CONVERTS THE FILE TO AN EXCEL FILE
'******************************************************
strFile = replace(strOrigFile,".csv",".xls")
 
Set objExcel = CreateObject("Excel.Application")
 
objExcel.Workbooks.Open strOrigFile
 
objExcel.DisplayAlerts = False
objExcel.ActiveWorkbook.SaveAs strFile, -4143
 
objExcel.DisplayAlerts = False
objExcel.ActiveWorkbook.Close
objExcel.DisplayAlerts = False
objExcel.Application.Quit
 
'******************************************************
'FORMAT THE EXCEL FILE AND SAVE IT
'******************************************************
 
objExcel.Workbooks.Open strFile
 
'Set the number format to zero D.P for specified columns
objExcel.columns("A:A").numberformat="0"
objExcel.columns("C:C").numberformat="0"
objExcel.columns("D:D").numberformat="0"
 
 
objExcel.DisplayAlerts = False
objExcel.ActiveWorkbook.SaveAs strFile, -4143
 
objExcel.DisplayAlerts = False
objExcel.ActiveWorkbook.Close
objExcel.DisplayAlerts = False
objExcel.Application.Quit
Set objExcel = Nothing
 
'msgbox "FINISHED!!!"
 
'******************************************************
'Delete the input file
'******************************************************
Set fso = CreateObject("Scripting.FileSystemObject")
fso.DeleteFile(strOrigFile)

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of sirbounty
sirbounty
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of arundelr
arundelr

ASKER

Thats Great - Thanks sirbounty ;o)
A+