I am running Windows 7 x64 and Office 2010 x64. I'm having an issue with VBA code for saving a file as .xlsx. If I save it as .xls, everything is fine, but I need to save it as .xlsx. Here is my code:
Sub Save_Alignment()
With Application
.ScreenUpdating = False
.EnableEvents = False
.DisplayAlerts = False
End With
Dim Fpath As String
Dim Fname As String
Dim FileExtStr As String
Dim FileFormatNum As Long
FileExtStr = ".xls"
FileFormatNum = 56
Fpath = "C:\Users\Me\Documents\"
Fname = "Document " & Format(Date, "yyyy-mm-dd") & FileExtStr
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets 'SetVersions
If ws.Name = "CFGBranchAlignment" Then
Dim wb As Workbook
Set wb = ws.Application.Workbooks.Add
ws.Copy Before:=wb.Sheets(1)
wb.SaveAs Fpath & Fname, FileFormat:=FileFormatNum
Set wb = Nothing
End If
Next ws
Workbooks("Document MASTER.xlsm").Activate
Workbooks(Fname).Close SaveChanges:=False
With Application
.ScreenUpdating = True
.EnableEvents = True
.DisplayAlerts = True
End With
End Sub
This will run fine, until I change the FileExtStr to .xlsx and the FileFormatNum to 51. Then the code breaks at wb.SaveAs Fpath... line.
I have also tried the following line after Fpath:
wb.SaveAs path & Fname, Excel.XlFileFormat.xlOpenXMLWorkbook
I am copying this from a macro enabled workbook (if that helps).
Got the basics for the code here:
Use VBA Macro to Save each Excel Worksheet as Separate Workbook
But I'm not able to get it to run with error unless I save the file in the Old excel version. At my wits end so any help would be appreciated.