簡體   English   中英

在 Excel for Mac 中打印到 PDF 的 VBA 代碼

[英]VBA Code to Print to PDF in Excel for Mac

我希望在 Excel for Mac 中自動打印為 PDF。 我曾嘗試使用宏記錄器,但由於某種原因它沒有將保存為 PDF 代碼。 我相信我對下面代碼的問題在於 Filename:= 片或 Quality:= 片。 命名約定可能會關閉,因為我通常不會在 Mac 上執行此操作,但現在沒有 PC。 建議將不勝感激!

見下面的代碼:

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

您可以在此處找到您的解決方案。 問題是mac的沙箱要求。

這是我正在使用的示例代碼。 歸功於 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

彼得

我正在努力解決同樣的問題,並找到了您的帖子。 我拿走了你的代碼並將其剝離到

 ActiveWindow.SelectedSheets.ExportAsFixedFormat Type:=xlTypePDF

這產生了一個錯誤,表明 SelectedSheets 不支持 ExportAsFixedFormat。

然后我將該行修改為

 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

這會產生 2 個 pdf 文件,但兩張紙​​都被完整打印,而不是只打印 PrintArea。 現在我很討厭,因為 Mac 上的 VBA 被證明是遲鈍的(或者證明我是,很難說是哪個)。

更新。 事實證明,我是那個昏暗的人。 我沒有檢查紙張以確保設置了它們的打印區域。 上面的代碼為每個選定的工作表生成一個 pdf 並僅導出 PrintArea。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM