What you are missing is that instead of calling wb.Close SaveChanges=True
to save the file in another format, you need to call wb.SaveAs
with the new file format and name.
You said you want to convert them without changing the file name, but I suspect you really meant you want to save them with the same base file name, but with the .xls
extension. So if the workbook is named book1.xlsx
, you want to save it as book1.xls
. To calculate the new name you can do a simple Replace()
on the old name replacing the .xlsx
extension with .xls
.
You can also disable the compatibility checker by setting wb.CheckCompatibility
, and suppress alerts and messages by setting Application.DisplayAlerts
.
Sub ProcessFiles()
Dim Filename, Pathname, saveFileName As String
Dim wb As Workbook
Dim initialDisplayAlerts As Boolean
Pathname = "<insert_path_here>" ' Needs to have a trailing \
Filename = Dir(Pathname & "*.xlsx")
initialDisplayAlerts = Application.DisplayAlerts
Application.DisplayAlerts = False
Do While Filename <> ""
Set wb = Workbooks.Open(Filename:=Pathname & Filename, _
UpdateLinks:=False)
wb.CheckCompatibility = False
saveFileName = Replace(Filename, ".xlsx", ".xls")
wb.SaveAs Filename:=Pathname & saveFileName, _
FileFormat:=xlExcel8, Password:="", WriteResPassword:="", _
ReadOnlyRecommended:=False, CreateBackup:=False
wb.Close SaveChanges:=False
Filename = Dir()
Loop
Application.DisplayAlerts = initialDisplayAlerts
End Sub