[英]VBA macro in Excel 2016 for Mac: SaveAs will not work with a CSV file format
I am running a VBA macro in Excel 2016 for Mac.我正在 Excel 2016 for Mac 中运行 VBA 宏。 The macro works on Windows platforms with Excel 2016, and on Mac platforms with earlier than the 2016 version.
该宏适用于使用 Excel 2016 的 Windows 平台以及早于 2016 版本的 Mac 平台。 The issue appears specific to Excel 2016 for Mac when trying to export a CSV.
尝试导出 CSV 时,该问题特定于 Excel 2016 for Mac。
The code is supposed to allow the user to click a button, which will then export an active worksheet to a CSV file.该代码应该允许用户单击一个按钮,然后将活动工作表导出到 CSV 文件。 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.虽然这里记录了一个类似的问题( 在尝试将 XLSM 保存为 CSV 时出现“Getting "method saveas of object _workbook failed" 错误),但不幸的是,虽然将
xlCSV
更改为6
对他们xlCSV
,但这对我不起作用。
The code works up until the ActiveWorkbook.SaveAs Filename:=newFileName, FileFormat:=6, CreateBackup:=False
line, which then throws the error:该代码一直运行到
ActiveWorkbook.SaveAs Filename:=newFileName, FileFormat:=6, CreateBackup:=False
行,然后抛出错误:
Run-time error '1004':Method 'SaveAs' of object '_Workbook' failed
运行时错误“1004”:对象“_Workbook”的方法“SaveAs”失败
If I change FileFormat
to 51
(.xlsx) or 53
(.xlsm) the code will successfully finish.如果我将
FileFormat
更改为51
(.xlsx) 或53
(.xlsm),代码将成功完成。 However, if FileFormat
is set to 6
(.csv) the code will throw the error above.但是,如果
FileFormat
设置为6
(.csv),则代码将抛出上述错误。 I am unable to SaveAs
xlCSV
or xlCSVMac
.我无法
SaveAs
xlCSV
或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. FileFormat
参数的各种文件类型,但如前所述,实际上只有两种类型有效。I Think its a bug, i have the same problem and i use a workaround:我认为这是一个错误,我有同样的问题,我使用了一种解决方法:
ActiveWorkbook.SaveAs newFileName, CreateBackup:=False
ActiveWorkbook.SaveAs newFileName, CreateBackup:=False
Kill newFileName杀死新文件名
ActiveWorkbook.SaveAs newFileName, FileFormat:=xlCSV, CreateBackup:=FalseActiveWorkbook.SaveAs newFileName, FileFormat:=xlCSV, CreateBackup:=False
ActiveWorkbook.CloseActiveWorkbook.Close
This save my Workbook first as original file (xlsx), delete this and save as csv, its works for me.这首先将我的工作簿保存为原始文件(xlsx),删除它并另存为 csv,它对我有用。
Try this:试试这个:
Application.ThisWorkbook.SaveAs ("C:\User\Folder\test.csv")
Also delete the line ActiveWorkbook.Save
.同时删除行
ActiveWorkbook.Save
。 You are double saving it.你是双重保存它。 I am using Excel 2010 and this work perfectly on my machine.
我正在使用 Excel 2010,这在我的机器上完美运行。
Try changing the permissions to explicitly request the new filename instead of the path by changing wsPath to newFileName:尝试通过将 wsPath 更改为 newFileName 来更改权限以显式请求新文件名而不是路径:
#If MAC_OFFICE_VERSION >= 15 Then
filePermissionCandidates = Array(newFileName)
fileAccessGranted = GrantAccessToMultipleFiles(filePermissionCandidates)
#End If
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.