繁体   English   中英

在 Mac 上导出为 PDF

[英]Export as PDF on a Mac

我有一个代码可以将每个单独的选项卡导出为 PDF 在 Excel 文件所在的同一文件夹中,它在 Windows 上按预期工作,但在 mac 上失败,错误“定义的对象”或应用程序定义错误一直在研究 VBA 在 Mac 上的使用,但似乎您需要将其保存在您需要之前访问的完全不同的文件夹位置,对此有什么解决方案吗? 它可以在同一个文件夹中工作,也可以在用户可以从打开的对话框 window 中使用 select 的文件夹中工作。 代码下方:

Option Explicit

Sub WorksheetLoop()

Dim wsA     As Worksheet
Dim wbA     As Workbook
Dim strTime As String
Dim strName As String
Dim strPath As String
Dim strFile As String
Dim strPathFile As String
Dim myFile  As Variant
Dim WS_Count As Integer
Dim I       As Integer
Dim fName As String

' Set WS_Count equal to the number of worksheets in the active workbook.
Set wbA = ActiveWorkbook
WS_Count = wbA.Worksheets.Count
strPath = wbA.Path
strTime = Format(Now(), "yyyymmdd\_hhmm")

'get active workbook folder, if saved
strPath = wbA.Path
If strPath = "" Then
    strPath = Application.DefaultFilePath
End If
strPath = strPath & "\"

' Begin the loop.
For Each wsA In wbA.Worksheets
    wsA.Activate
    'replace spaces and periods in sheet name
    strName = Replace(wsA.Name, " ", "")
    strName = Replace(strName, ".", "_")

    'create default name for savng file
    strFile = ActiveSheet.Range("D5").Value & ".pdf"
    myFile = strPath & strFile

    Debug.Print myFile

    'export to PDF if a folder was selected
    If myFile <> "False" Then
        ActiveSheet.ExportAsFixedFormat _
                Type:=xlTypePDF, _
                Filename:=myFile, _
                Quality:=xlQualityStandard, _
                IncludeDocProperties:=True, _
                IgnorePrintAreas:=False, _
                OpenAfterPublish:=False
        'confirmation message with file info
       
    End If

Next wsA
 MsgBox "Your PDF files have been created: "
 Worksheets("Summary").Activate
               
End Sub

这听起来像是 Mac 上的“沙盒”问题。 从 Office 2016 开始,Apple 让 Microsoft 限制 Excel VBA 可以保存文件的文件夹。

我不确定你想如何调整你的代码以使其工作,但 Ron de Bruin 有一个关于 Excel VBA 的很棒的网站,还有一个关于在 Mac 上执行此操作的特别有用的部分。 这个特定页面讨论了沙盒问题,并解释了您应该能够将文件保存到哪些特定文件夹而不会出现任何问题。

https://www.rondebruin.nl/mac/mac034.htm

暂无
暂无

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

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