Link to home
Start Free TrialLog in
Avatar of hennessym
hennessym

asked on

VBScript: Save .CSV as .TSV

Hi folks,

I'm pulling my hair out on this one:

Can anyone tell me why this script successfully saves my .CSV as a .TSV:

Call CSVtoTSV("c:\files\file1.CSV")

Private Function CSVtoTSV(ByVal Filename)

'The CSV file to be converted
strInput = Filename
 
'The Excel file to be created
strOutput = left(Filename,InStr(Filename,".")-1) + ".TSV"
Debug.Write strOutput

Set objExcel = CreateObject("Excel.Application")
objExcel.DisplayAlerts = FALSE
objExcel.Visible = FALSE
 
Set objWorkbook = objExcel.Workbooks.Open(strInput)
Set objWorksheet = objWorkbook.Worksheets(1)
 
objExcel.ActiveWorkbook.SaveAs strOutput, -4158
objExcel.ActiveWorkbook.Close
objExcel.Application.Quit

End Function

..but using very similar code and calling the script from SSMS produces the error: ActiveX Component Can't Create Object Excel.Application?

Select Case WScript.Arguments.Count
Case 1: Arg = WScript.Arguments(0)
End Select

Call CSVtoTSV(Arg)

Private Function CSVtoTSV(ByVal Filename)

'The CSV file to be converted
strInput = Filename
 
'The Excel file to be created
strOutput = left(Filename,InStr(Filename,".")-1) + ".TSV"
Debug.Write strOutput

Set objExcel = CreateObject("Excel.Application")
objExcel.DisplayAlerts = FALSE
objExcel.Visible = FALSE
 
Set objWorkbook = objExcel.Workbooks.Open(strInput)
Set objWorksheet = objWorkbook.Worksheets(1)
 
objExcel.ActiveWorkbook.SaveAs strOutput, -4158
objExcel.ActiveWorkbook.Close
objExcel.Application.Quit

End Function

...and I'm calling it from SSMS:
EXEC master..xp_cmdshell 'CScript "c:\files\test.vbs" "c:\files\file1.csv"''

Any help would be greatly appreciated.

Thanks.
ASKER CERTIFIED SOLUTION
Avatar of Anthony Perkins
Anthony Perkins
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
Or the script isn't running under a security context with enough rights to use ActiveX controls, or the task isn't running interactively.  Excel automation *may* require the task to be interactive, I can't remember...
Avatar of hennessym
hennessym

ASKER

Of course that's the answer - what was I thinking?!  Thanks, acperkins.