简体   繁体   中英

Save as pdf using Mac Excel VBA

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'.

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

I have been battling with this issue myself when creating an automatic Excel Invoicing tool. And the biggest issue was simply that in the company I work at, everyone uses Macs, so I had to get creative. And unfortunately there's not much help online (I also checked out rondebruin website which is mentioned a lot but to no avail), but this has worked for me and I think it should work for you and anyone who will come at a later date.

To fix Excel not letting you save to your specific folder simply record a macro and do the steps you require normally. In my case they were: Save As, select PDF (in the Dialog box), Select your folder (again in the Dialog box). Once this is done open VBA and look at the code it has just given you for that recorded Macro. Now you simply have to change the details you need, like for example the Name of the Pdf based on cell value or sheet name and that's it. Once you execute your macro, macOS will ask you to grant permission to excel which you simply accept and best of all is you only have to do it once and then it works automatically.

Here's a snippet of the code I used which works on macOS Big Sur Version 11.6 and Excel Version 16.54:

Sub Save_Invoice_to_Folder()

ChDir _
    "/Users/Example/Downloads/"
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, filename:= _
    "/Users/Example/Downloads/" & "Invoice-" & Range("E3").Text & ".pdf" _
    , Quality:=xlQualityMinimum, IncludeDocProperties:=True, IgnorePrintAreas _
    :=False, OpenAfterPublish:=False

End Sub

Obviously replace "Example" with your username and add your folder path for your specific case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM