简体   繁体   中英

VBA Code to Print to PDF in Excel for Mac

I am looking to automate printing to PDF in Excel for Mac. I have tried to use the Macro Recorder, but for some reason it doesn't save the save to PDF code. I believe that my issue with the code below is with the Filename:= piece or the Quality:= piece. The name convention may be off since I don't normally do this on a mac, but don't have a PC now. Suggestions would be much appreciated!

See code below:

Sheets("Output").Select
Range("A1:P57").Select
ActiveSheet.PageSetup.PrintArea = "$A$1:$P$57"
With ActiveSheet.PageSetup
    .Orientation = xlLandscape
    .FitToPagesWide = 1
    .FitToPagesTall = 1
End With
ActiveWindow.SelectedSheets.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:="Users/MyName/Desktop/Cash Flow 1.pdf" _
Quality:=xlQualityMedium, IncludeDocProperties:=False, _
IgnorePrintAreas:=False, OpenAfterPublish:=True

your solution can be found here. The issue are the sandbox-requirements of mac.

Here my sample code I'm Using. Credits to Rondebruin

Sub ExportasPdf(Druckbereich As Range, Dokumentname As String)
' See https://www.rondebruin.nl/mac/mac034.htm
OfficeFolder = MacScript("return POSIX path of (path to desktop folder) as string")
OfficeFolder = Replace(OfficeFolder, "/Desktop", "") & "Library/Group Containers/UBF8T346G9.Office/" & "MyFolder/"
str_Filename = OfficeFolder & Dokumentname & ".pdf"
'MsgBox (str_filename)
Druckbereich.ExportAsFixedFormat Type:=xlTypePDF, FileName:=str_Filename _
    , Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False _
    , OpenAfterPublish:=False

End Sub

Peter

I'm struggling with the same issue and found your post. I took your code and stripped it down to

 ActiveWindow.SelectedSheets.ExportAsFixedFormat Type:=xlTypePDF

That produced an error indicating SelectedSheets didn't support ExportAsFixedFormat.

I then modified the line to

 ix = 1
 For Each sh In ActiveWindow.SelectedSheets
     sh.ExportAsFixedFormat Type:=xlTypePDF, _ 
      fileName:=ThisWorkbook.path & "boo-" & ix & ".pdf", IgnorePrintAreas:=False
     ix = ix+1
  Next sh

That produces 2 pdf files but both sheets are printed in their entirety instead of only printing the PrintArea. Now I'm obnoxed because VBA on the Mac is proving to be obtuse (or is proving me to be, hard to tell which).

update. As it turned out, it I was the dim one. I didn't check the sheets to ensure their print areas were set. The above code produces a pdf for each selected sheet and exports just the PrintArea.

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