【问题标题】:Export as PDF on a Mac在 Mac 上导出为 PDF
【发布时间】:2020-06-29 19:11:11
【问题描述】:

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

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 上做路径。
  • 谢谢你,迈克,似乎找不到你的问题,因为我也做了一些研究,但找不到与 Mac 文件夹相同路径的方法跨度>
  • 嗯,我这里只有 9 个问题,所以这应该不是太大的挑战......但是这里给你一个想法,如果它给你一些帮助,给它投票:stackoverflow.com/q/30575923/4961700

标签: excel vba macos


【解决方案1】:

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

我不确定确切您想如何调整代码以使其正常工作,但 Ron de Bruin 有一个很棒的关于 Excel VBA 的网站,以及关于在苹果电脑。这个特定页面讨论了沙盒问题,并解释了您应该能够将文件保存到哪些特定文件夹而不会出现任何问题。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-12
    • 2017-04-03
    • 1970-01-01
    • 2013-06-23
    • 2021-12-05
    • 1970-01-01
    相关资源
    最近更新 更多