1

我在一个文件夹中有几个“.xls”文件,需要将其转换为制表符分隔值为此找到了一个 vb 脚本..请一些人建议如何做到这一点?运行此程序时出现几个错误。我不是 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.
4

1 回答 1

2

您的问题是您正在尝试使用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()
于 2013-08-01T12:59:21.677 回答