将xls文件转换为制表符分隔文件的异常

时间:2013-08-01 12:00:02

标签: vb.net vba excel-vba excel

我在文件夹上有几个“.xls”文件,需要将其转换为制表符分隔值 找到一个vb脚本为此..请一些身体sugest如何做到这一点? 我在运行时遇到了一些错误。我不是vb程序员。专家......请帮忙

Public Sub Main()
    Dim WScript As Object = Nothing   '' with out nothing it was showing an error
    Dim oExcel As Object
    Dim oBook As Object

   If WScript.Arguments.Count < 2 Then
      WScript.Echo("Error! Please specify the source path and the destination. Usage: XlsToCsv SourcePath.xls Destination.csv")
      Wscript.Quit()
   End If
   oExcel = CreateObject("Excel.Application")
   oBook = oExcel.Workbooks.Open(WScript.Arguments.Item(0))  ''item o might be excel
   oBook = oExcel.Workbooks.Open("C:\Users\5A5.xls")
   oBook.SaveAs(WScript.Arguments.Item(1), -4158)

   oBook.Close(False)
   oExcel.Quit()
   WScript.Echo("Done")

End Sub

例外:

System.Reflection.TargetInvocationException:    
   Exception has been thrown by the target of an invocation. 
     ---> System.NullReferenceException: Object variable or With block variable not set.

1 个答案:

答案 0 :(得分:1)

您的问题是您正在尝试使用WScript但它尚未初始化(它已设置为Nothing)。

没有它的情况下尝试:

   Dim oExcel As Object
   Dim oBook As Object

   oExcel = CreateObject("Excel.Application")
   oBook = oExcel.Workbooks.Open("C:\Users\5A5.xls")
   oBook.SaveAs("C:\Users\5A5.txt", -4158)

   oBook.Close(False)
   oExcel.Quit()