【问题标题】:VBA Code to Print to PDF in Excel for Mac在 Excel for Mac 中打印到 PDF 的 VBA 代码
【发布时间】:2019-09-02 14:06:27
【问题描述】:

我希望在 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

【问题讨论】:

    标签: excel vba pdf printing


    【解决方案1】:

    您的解决方案可以在这里找到。问题是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
    

    彼得

    【讨论】:

      【解决方案2】:

      我正在努力解决同样的问题,并找到了您的帖子。我拿走了你的代码并将其剥离为

       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。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-06-09
        • 2017-10-25
        • 2015-02-14
        • 2013-11-09
        • 1970-01-01
        • 1970-01-01
        • 2019-02-24
        相关资源
        最近更新 更多