Kat*_*ate 5 macos macros excel vba excel-2016
我正在 Excel 2016 for Mac 中运行 VBA 宏。该宏适用于使用 Excel 2016 的 Windows 平台和早于 2016 版本的 Mac 平台。尝试导出 CSV 时,该问题特定于 Excel 2016 for Mac。
该代码应该允许用户单击按钮,然后将活动工作表导出到 CSV 文件。虽然这里记录了一个类似的问题(在尝试将 XLSM 保存为 CSV 时出现“Getting "method saveas of object _workbook failed" 错误),但不幸的是,在更改xlCSV为6为他们工作时,这对我不起作用。
该代码一直运行到该ActiveWorkbook.SaveAs Filename:=newFileName, FileFormat:=6, CreateBackup:=False行,然后抛出错误:
运行时错误“1004”:对象“_Workbook”的方法“SaveAs”失败
如果我更改FileFormat为51(.xlsx) 或53(.xlsm),代码将成功完成。但是,如果FileFormat设置为6(.csv),则代码将抛出上述错误。我无法SaveAs xlCSV或xlCSVMac。
我的完整脚本如下:
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
Run Code Online (Sandbox Code Playgroud)
我试过了:
FileFormat参数的各种文件类型,但如前所述,实际上只有两种类型有效。| 归档时间: |
|
| 查看次数: |
5636 次 |
| 最近记录: |