当尝试在装有MS Office 365的PC上运行以下脚本时,它工作正常,但如果我尝试在装有MS Office 2003的PC上运行相同的脚本,则会出现错误。
Windows脚本主机
第36行
字符17
Workbook类的错误SaveAs方法失败
代码800A03EC
来源Miscrosoft Office Excel
目标:每隔一分钟将指定文件夹中的所有CSV文件转换为XLS。如上所述,它在装有Office 365的计算机上运行良好,但是如果我在装有Office 2003的计算机上运行它,则会发出错误消息。他们都是独立的机器。请帮助我解决此兼容性问题,以便我可以在装有MS Office 2003的计算机上运行此脚本。
Dim waittime: waittime = 1 * 60 * 1000
'Constants
Const xlOpenXMLWorkbook = 51 '(without macro's in 2007-2016, xlsx)
Const xlOpenXMLWorkbookMacroEnabled = 52 '(with or without macro's in 2007-2016, xlsm)
Const xlExcel12 = 50 '(Excel Binary Workbook in 2007-2016 with or without macro's, xlsb)
Const xlExcel8 = 56
Do
' Extensions for old and new files
strExcel = "xls"
strCSV = "csv"
' Set up filesystem object for usage
Set objFSO = CreateObject("Scripting.FileSystemObject")
' Access the folder to process
Set objFolder = objFSO.GetFolder("C:\Users\User\Desktop\CSV to XL")
' Load Excel (hidden) for conversions
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = False
objExcel.DisplayAlerts = False
' Process all files
For Each objFile In objFolder.Files
' Get full path to file
strPath = objFile.Path
' Only convert CSV files
If LCase(objFSO.GetExtensionName(strPath)) = LCase(strCSV) Then
' Display to console each file being converted
'WScript.Echo "Converting """ & strPath & """"
' Load CSV into Excel and save as native Excel file
Set objWorkbook = objExcel.Workbooks.Open(strPath, False, True)
objWorkbook.SaveAs Replace(strPath, strCSV, strExcel), xlOpenXMLWorkbook
objWorkbook.Close False
Set objWorkbook = Nothing
End If
Next
WScript.Sleep (waittime)
Loop
答案 0 :(得分:0)
xlOpenXMLWorkbook
枚举的xlFileFormat
值会将文件另存为Open XML工作簿,仅从Office 2007开始受支持。现在,由于要以Excel 2003文件格式保存它,因此应该为该使用适当的值,在这种情况下,该值为xlExcel8
。
以下是xlFileFormat
枚举的所有值的列表:
xlFileFormat enumeration (Excel)。
因此,您需要做的是将对xlOpenXMLWorkbook
方法的调用中的SaveAs
替换为xlExcel8
:
objWorkbook.SaveAs outputFilePath, xlExcel8
编辑:
如果仅安装了Office 2003,则似乎不支持xlExcel8
,因此您需要使用xlExcel9795
。首先,将此常量添加到文件顶部:
Const xlExcel9795 = 43
然后,您可以将其传递给SaveAs
方法,如下所示:
objWorkbook.SaveAs outputFilePath, xlExcel9795