2013-08-01 87 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:您打賭。請記住標記答案,如果它對你有幫助。 –

相關問題