将带分隔符的文本文件通过管道传递到vbscript

时间:2018-11-05 18:36:48

标签: excel text vbscript tab-delimited-text

我有一个带有|的txt文件。定界的。我想将该文件保存在Excel中。这是我的代码

' Define constants
Const xlDelimited = 1   
'The TSV file to be converted
strProdTextFile = "C:\Users\laxmi\Documents\Payroll\CompareFiles\SEACRDPRD01SVM_xLearnCom_10292018.txt"

'The Excel file to be created
strProdExcelFile = "C:\Users\laxmi\Documents\Payroll\CompareFiles\SEACRDPRD01SVM_xLearnCom_10292018.xls"

Set objExcel = CreateObject("Excel.Application")
objExcel.DisplayAlerts = FALSE
objExcel.Visible = FALSE

Set objWorkbook = objExcel.Workbooks.OpenText(strProdTextFile,,,xlDelimited,,,,,,,True,"|")
objExcel.ActiveWorkbook.SaveAs strProdExcelFile, 51

objExcel.ActiveWorkbook.Close False
objExcel.ActiveWorkbook.Close
objExcel.Application.Quit 

这是我得到的错误:

  

所需对象:'objExcel.Workbooks.OpenText(...)'

     

第14行:“设置objWorkbook = objExcel.Workbooks.OpenText(strProdTextFile ,,, xlDelimited ,,,,,, True,“ |”)“

1 个答案:

答案 0 :(得分:0)

Workbooks.OpenText没有返回值,因此您的脚本在

上失败
Set objWorkbook = objExcel.Workbooks.OpenText(...)

因为Set objWorkbook =在右侧需要一个对象。由于您的下一行正在使用ActiveWorkbook,因此您只需从行首删除Set objWorkbook =