0

我有一个 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
4

1 回答 1

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

我希望这会有所帮助

菲利普

于 2013-03-15T14:52:00.000 回答