Reputation: 133
I am running a VBA macro in Excel 2016 for Mac. The macro works on Windows platforms with Excel 2016, and on Mac platforms with earlier than the 2016 version. The issue appears specific to Excel 2016 for Mac when trying to export a CSV.
The code is supposed to allow the user to click a button, which will then export an active worksheet to a CSV file. While a similar issue was documented here (Getting "method saveas of object _workbook failed" error while trying to save an XLSM as CSV), unfortunately while changing xlCSV
to 6
worked for them, this has not worked for me.
The code works up until the ActiveWorkbook.SaveAs Filename:=newFileName, FileFormat:=6, CreateBackup:=False
line, which then throws the error:
Run-time error '1004':Method 'SaveAs' of object '_Workbook' failed
If I change FileFormat
to 51
(.xlsx) or 53
(.xlsm) the code will successfully finish. However, if FileFormat
is set to 6
(.csv) the code will throw the error above. I am unable to SaveAs
xlCSV
or xlCSVMac
.
My full script is below:
Sub btnExportCSV_Click()
Dim oldFileName As String
Dim newFileName As String
Dim timeStamp As String
Dim fileAccessGranted As Boolean
Dim filePermissionCandidates
Dim wsPath As String
timeStamp = Format(Now, "yyyymmddhhmmss")
wsPath = Application.ThisWorkbook.Path
oldFileName = ThisWorkbook.FullName
newFileName = Mid(oldFileName, 1, InStrRev(oldFileName, ".") - 1) & timeStamp & ".csv"
' Check if software is Office 2016 for Mac
' Documentation for this comes from https://dev.office.com/blogs/VBA-improvements-in-Office-2016
#If MAC_OFFICE_VERSION >= 15 Then
filePermissionCandidates = Array(wsPath)
fileAccessGranted = GrantAccessToMultipleFiles(filePermissionCandidates)
#End If
Application.DisplayAlerts = False
Sheets("OfflineComments").Activate
Sheets("OfflineComments").Copy
ActiveWorkbook.SaveAs Filename:=newFileName, FileFormat:=6, CreateBackup:=False
ActiveWorkbook.Save
ActiveWindow.Close
MsgBox ("Offline comments exported to " & newFileName)
Application.DisplayAlerts = True
End Sub
I have tried:
FileFormat
parameter, but as mentioned, only two types actually worked.Upvotes: 4
Views: 7278
Reputation: 11
I Think its a bug, i have the same problem and i use a workaround:
ActiveWorkbook.SaveAs newFileName, CreateBackup:=False
Kill newFileName
ActiveWorkbook.SaveAs newFileName, FileFormat:=xlCSV, CreateBackup:=False
ActiveWorkbook.Close
This save my Workbook first as original file (xlsx), delete this and save as csv, its works for me.
Upvotes: 1
Reputation: 11
Try changing the permissions to explicitly request the new filename instead of the path by changing wsPath to newFileName:
#If MAC_OFFICE_VERSION >= 15 Then
filePermissionCandidates = Array(newFileName)
fileAccessGranted = GrantAccessToMultipleFiles(filePermissionCandidates)
#End If
Upvotes: 0
Reputation: 130
Try this:
Application.ThisWorkbook.SaveAs ("C:\User\Folder\test.csv")
Also delete the line ActiveWorkbook.Save
. You are double saving it.
I am using Excel 2010 and this work perfectly on my machine.
Upvotes: 0