【发布时间】:2021-04-29 22:45:18
【问题描述】:
我正在尝试遍历文件夹中的所有“xlsx”文件并将它们转换为“xls”(Excel 97-2003 工作表)格式。我使用以下代码,但输出文件仍保存为“xlsx”而不是“xls”。我是初学者,希望向他人学习更多。感谢您的帮助!
Sub Convert()
Dim strPath As String
Dim strFile As String
Dim strfilenew As String
Dim xWbk As Workbook
Dim xSFD, xRFD As FileDialog
Dim xSPath As String
Dim xRPath As String
Set xSFD = Application.FileDialog(msoFileDialogFolderPicker)
With xSFD
.Title = "Please select the folder contains the xls files:"
.InitialFileName = "C:\"
End With
If xSFD.Show <> -1 Then Exit Sub
xSPath = xSFD.SelectedItems.Item(1)
Set xRFD = Application.FileDialog(msoFileDialogFolderPicker)
With xRFD
.Title = "Please select a folder for outputting the new files:"
.InitialFileName = "C:\"
End With
If xRFD.Show <> -1 Then Exit Sub
xRPath = xRFD.SelectedItems.Item(1) & "\"
strPath = xSPath & "\"
strFile = Dir(strPath & "*.xlsx")
strfilenew = Dir(strPath & "*.xls")
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Do While strFile <> ""
If Right(strFile, 4) = "xlsx" Then
Set xWbk = Workbooks.Open(Filename:=strPath & strfilenew)
xWbk.SaveAs Filename:=xRPath & strfilenew, _
FileFormat:=xlExcel18
xWbk.Close SaveChanges:=True
End If
strFile = Dir
Loop
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
【问题讨论】:
-
XLS 文件的文件格式是
Excel8,而不是Excel18- 8 而不是 18。 -
感谢您的指出。我将其更改为 Excel8 并重新运行,但仍然没有运气。
-
在保存文件时尝试使用数字常量:
xWbk.SaveAs Filename := xRPath & strfilenew, FileFormat := 52。见rondebruin.nl/win/s5/win001.htm