[英]VBA code print to pdf in Excel on Mac (Landscape and file destination)
[英]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.