简体   繁体   English

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

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

I am looking to automate printing to PDF in Excel for Mac.我希望在 Excel for Mac 中自动打印为 PDF。 I have tried to use the Macro Recorder, but for some reason it doesn't save the save to PDF code.我曾尝试使用宏记录器,但由于某种原因它没有将保存为 PDF 代码。 I believe that my issue with the code below is with the Filename:= piece or the Quality:= piece.我相信我对下面代码的问题在于 Filename:= 片或 Quality:= 片。 The name convention may be off since I don't normally do this on a mac, but don't have a PC now.命名约定可能会关闭,因为我通常不会在 Mac 上执行此操作,但现在没有 PC。 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.问题是mac的沙箱要求。

Here my sample code I'm Using.这是我正在使用的示例代码。 Credits to Rondebruin归功于 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.这产生了一个错误,表明 SelectedSheets 不支持 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.这会产生 2 个 pdf 文件,但两张纸​​都被完整打印,而不是只打印 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).现在我很讨厌,因为 Mac 上的 VBA 被证明是迟钝的(或者证明我是,很难说是哪个)。

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.上面的代码为每个选定的工作表生成一个 pdf 并仅导出 PrintArea。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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