Solved

# Conditional check in VbScript

Posted on 2008-06-10
910 Views
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

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.ActiveWorkbook.SaveAs strFile, -4143

objExcel.ActiveWorkbook.Close
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.ActiveWorkbook.SaveAs strFile, -4143

objExcel.ActiveWorkbook.Close
objExcel.Application.Quit
Set objExcel = Nothing

'msgbox "FINISHED!!!"

'******************************************************
'Delete the input file
'******************************************************
Set fso = CreateObject("Scripting.FileSystemObject")
fso.DeleteFile(strOrigFile)

0
Question by:arundelr
• 2

LVL 67

Accepted Solution

sirbounty earned 500 total points
ID: 21752129
Put your fso assignment near the top...
Set fso = CreateObject("Scripting.FileSystemObject")

Then, just before line 23...

If not fso.FileExists(strOrigFile) Then
msgbox "Sorry. the file could not be located."
wscript.quit
End If
objExcel.Workbooks.Open strOrigFile
0

Author Comment

ID: 21752241
Thats Great - Thanks sirbounty ;o)
0

Author Closing Comment

ID: 31465766
A+
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…