2013-08-01 49 views
0

我在文件夹中有几个“.xls”文件,需要将其转换为制表符分隔值 找到了一个用于此的vb脚本..请使用某些主体sugest如何执行此操作? 我得到几个错误时运行this.I我不是一个VB programmer.Experts ......请帮助将xls文件转换为制表符分隔文件的异常

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. 
+0

你可能已经至少表明,这些“错误” ...:P – AKDADEVIL

+0

错误:System.Reflection.TargetInvocationException:异常已通过调用的目标引发异常。 ---> System.NullReferenceException:对象变量或未设置块变量。 – user1254579

回答

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() 
+0

感谢loooooot的帮助 – user1254579

+0

@ user1254579:您打赌。请记住标记答案,如果它对你有帮助。 –

相关问题