问题
I am struggling to export my worksheet as PDF.
Here is my code, FYI I am using Office 2016 for MAC
Sub saveactiveworkbook()
Dim FileName As String
Dim FolderName As String
Dim Folderstring As String
Dim FilePathName As String
ActiveSheet.PageSetup.Orientation = xlLandscape
FolderName = "TestVBA"
FileName = "DV" & ".pdf"
Folderstring = "/Users/Vallier/Documents/Test/TestVBA"
FilePathName = Folderstring & Application.PathSeparator & FileName
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:= _
FilePathName, Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False
MsgBox FilePathName
End Sub
When I launch the program I have a warning box that says: "Run-time Error '1004':
Application-Defined or object-defined error"
I try to remedy to this by defining active sheet by "ws" upfront but I then have another error saying:
"ExportAsFormat of object_ worksheet failed"
Any ideas?
Many Thanks,
回答1:
The problem comes from Apple's sandboxing rules. You can no longer save or open directly from VBA to any folder that you want. You may only write to a folder that Excel has rights to. This probably includes exporting a PDF, although I have not tested.
Here is an excellent page by Ron de Bruin detailing how to deal with Apple's sandboxing issues in VBA.
https://www.rondebruin.nl/mac/mac034.htm
来源:https://stackoverflow.com/questions/49029182/vba-export-worksheet-as-pdf-mac-2016-error