用于将excel转换为文本出口的VB脚本,错误代码为9009

时间:2013-03-15 14:06:03

标签: vb.net excel-vba vbscript vba excel

我有一个VB脚本,我将Excel(.xls)文件转换为文本文件。 它以错误代码9009退出。

我在安装了Excel 2003的服务器上运行此脚本,但不是整个MS Office。

以下是脚本

'Script Details:
'The script converts an excel file into a tab-delimited file
'The script requires two parameters - the source file name and target file name
'Author Maulik

'-----------------------------------------------------------------------------------
'Script checks and exectues only if there are 2 parameters
'-----------------------------------------------------------------------------------

if WScript.Arguments.Count < 2 Then
    WScript.Echo "Please Specify the Source File. Usage: ExcelToCsv <xls/xlsx source file>"
    Wscript.Quit 4
End If


txt_format = 20

Set objFSO = CreateObject("Scripting.FileSystemObject")

src_path = Wscript.Arguments.Item(0)

'WScript.Echo src_path

src_file = Wscript.Arguments.Item(1)

'WScript.Echo src_file

full_file_path = src_path & "\" & src_file

'WScript.Echo full_file_path


Dim xlApp
Dim xlBook
Dim xlSheet
Dim strOutputFileName

Set xlApp = CreateObject("Excel.Application")

xlApp.Visible = False
xlApp.EnableEvents = False

Set xlBook = xlApp.Workbooks.Open(full_file_path)

For Each xlSheet In xlBook.Worksheets

With xlSheet
strOutputFileName = full_file_path & "." & xlSheet.Name & ".dat"    
xlSheet.SaveAs strOutputFileName, txt_format
End With

Next

xlBook.Close False

xlApp.Quit

1 个答案:

答案 0 :(得分:0)

我认为该错误是错误的文件名或数字,因为您的路径中有空格。

尝试以下代码:

'Script Details:
'The script converts an excel file into a tab-delimited file
'The script requires two parameters - the source file name and target file name
'Author Maulik

'-----------------------------------------------------------------------------------
'Script checks and exectues only if there are 2 parameters
'-----------------------------------------------------------------------------------

if WScript.Arguments.Count < 2 Then
    WScript.Echo "Please Specify the Source File. Usage: ExcelToCsv <xls/xlsx source file>"
    Wscript.Quit 4
End If


txt_format = 6 ' xlCSV is 6, 20 is xlTextWindows !

Set objFSO = CreateObject("Scripting.FileSystemObject")

src_path = Wscript.Arguments.Item(0)

'WScript.Echo src_path

src_file = Wscript.Arguments.Item(1)

'WScript.Echo src_file

full_file_path = src_path & "\" & src_file

'WScript.Echo full_file_path


Dim xlApp
Dim xlBook
Dim xlSheet
Dim strOutputFileName

Set xlApp = CreateObject("Excel.Application")

xlApp.Visible =true ' false
xlApp.EnableEvents = False

Set xlBook = xlApp.Workbooks.Open(full_file_path)

For Each xlSheet In xlBook.Worksheets

With xlSheet
strOutputFileName = """" & full_file_path & "." & xlSheet.Name & ".dat" & """"   
xlSheet.SaveAs strOutputFileName, txt_format
End With

Next

xlBook.Close False

xlApp.Quit

我希望有帮助

菲利普