问题
My Excel VBA on Mac OS to save as PDF sometimes doesn't work.
It will give
"Error while printing"
then
"Run-time error '1004': Application-defined or object-defined error".
My code, which worked yesterday:
'a) For Windows
Dim wksSheet As Worksheet, PS As String
PS = Application.PathSeparator
If InStr(1, Application.OperatingSystem, "Windows") > 0 Then
Set wksSheet = ActiveSheet
wksSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:= _
ThisWorkbook.Path & PS & pdfName, Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
False
'Exit Sub
Else
'b) For MAC
Dim pdfName As String, FullName As String
pdfName = "Q - " & Range("F2").Value & " - " & Range("A2").Value & " - " & Range("B10").Text
FullName = "/Users/" & QTGetUserNameMAC & "/Google Drive/ABC Pte Ltd/Q - Quotations/" _
& pdfName & ".pdf"
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:=FullName _
, Quality:=xlQualityMinimum, IncludeDocProperties:=True, IgnorePrintAreas _
:=False, OpenAfterPublish:=False
End If
I recorded the macro and used the code generated in my code (as below). It works. However, if I delete the last '9' in the code, I get the above error.
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:= _
"/Users/" & QTGetUserNameMAC & "/Google Drive/ABC Pte Ltd/Q - Quotations/Q - VAS-Quote-QT190039.pdf" _
, Quality:=xlQualityMinimum, IncludeDocProperties:=True, IgnorePrintAreas _
:=False, OpenAfterPublish:=False
I tried using PathSeparator, and again, with the last '9' it works but gives an error without the '9'.
回答1:
Here is a guide on how you can do it: https://www.rondebruin.nl/mac/mac034.htm
What's important is that you can't save your file to a location that is selected by you.
It has to be saved to the folder Library/Group Containers/UBF8T346G9.Office under the current user's home dir, so /Users/[current user]/Library/Group Containers/UBF8T346G9.Office in most of the cases. If the folder is not there, you have to create it. (See the code example on the page linked above)
Kudos to Ron!
Please vote for this to be fixed by MS here: https://excel.uservoice.com/forums/304933-excel-for-mac/suggestions/36531559-fix-exportasfixedformat-method
来源:https://stackoverflow.com/questions/52473104/save-as-pdf-using-mac-excel-vba