0

Excel(.xls)ファイルをテキストファイルに変換するVBスクリプトがあります。エラーコード9009で終了します。

このスクリプトは、Excel 2003がインストールされているサーバーで実行していますが、MSOffice全体では実行していません。

以下はスクリプトです

'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 に答える