简体   繁体   English

使用 Mac Excel VBA 保存为 pdf

[英]Save as pdf using Mac Excel VBA

My Excel VBA on Mac OS to save as PDF sometimes doesn't work.我在 Mac OS 上保存为 PDF 的 Excel VBA 有时不起作用。

It will give它会给

"Error while printing" “打印时出错”

then然后

"Run-time error '1004': Application-defined or object-defined error". “运行时错误‘1004’:应用程序定义或对象定义的错误”。

My code, which worked yesterday:我的代码,昨天有效:

'a) For Windows

Dim wksSheet As Worksheet, PS As String

PS = Application.PathSeparator

If InStr(1, Application.OperatingSystem, "Windows") > 0 Then
    Set wksSheet = ActiveSheet
    wksSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:= _
      ThisWorkbook.Path & PS & pdfName, Quality:=xlQualityStandard, _
      IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
      False
    'Exit Sub

Else

'b) For MAC

    Dim pdfName As String, FullName As String

    pdfName = "Q - " & Range("F2").Value & " - " & Range("A2").Value & " - " & Range("B10").Text
    FullName = "/Users/" & QTGetUserNameMAC & "/Google Drive/ABC Pte Ltd/Q - Quotations/" _
      & pdfName & ".pdf"

    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:=FullName _
      , Quality:=xlQualityMinimum, IncludeDocProperties:=True, IgnorePrintAreas _
      :=False, OpenAfterPublish:=False

End If

I recorded the macro and used the code generated in my code (as below).我记录了宏并使用了我的代码中生成的代码(如下所示)。 It works.有用。 However, if I delete the last '9' in the code, I get the above error.但是,如果我删除代码中的最后一个“9”,则会出现上述错误。

ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:= _
  "/Users/" & QTGetUserNameMAC & "/Google Drive/ABC Pte Ltd/Q - Quotations/Q - VAS-Quote-QT190039.pdf" _
  , Quality:=xlQualityMinimum, IncludeDocProperties:=True, IgnorePrintAreas _
  :=False, OpenAfterPublish:=False

I tried using PathSeparator, and again, with the last '9' it works but gives an error without the '9'.我尝试使用 PathSeparator,并且再次使用最后一个 '9' 它可以工作,但是没有 '9' 会出现错误。

Here is a guide on how you can do it: https://www.rondebruin.nl/mac/mac034.htm以下是有关如何执行此操作的指南: https : //www.rondebruin.nl/mac/mac034.htm

What's important is that you can't save your file to a location that is selected by you.重要的是您无法将文件保存到您选择的位置。

It has to be saved to the folder Library/Group Containers/UBF8T346G9.Office under the current user's home dir, so /Users/[current user]/Library/Group Containers/UBF8T346G9.Office in most of the cases.它必须保存到当前用户主目录下的文件夹Library/Group Containers/UBF8T346G9.Office ,因此在大多数情况下是/Users/[current user]/Library/Group Containers/UBF8T346G9.Office If the folder is not there, you have to create it.如果该文件夹不存在,则必须创建它。 (See the code example on the page linked above) (请参阅上面链接页面上的代码示例)

Kudos to Ron!为罗恩点赞!

Please vote for this to be fixed by MS here: https://excel.uservoice.com/forums/304933-excel-for-mac/suggestions/36531559-fix-exportasfixedformat-method请在此处投票支持 MS 修复此问题: https : //excel.uservoice.com/forums/304933-excel-for-mac/suggestions/36531559-fix-exportasfixedformat-method

I have been battling with this issue myself when creating an automatic Excel Invoicing tool.在创建自动 Excel 发票工具时,我自己一直在与这个问题作斗争。 And the biggest issue was simply that in the company I work at, everyone uses Macs, so I had to get creative.最大的问题很简单,在我工作的公司,每个人都使用 Mac,所以我必须发挥创造力。 And unfortunately there's not much help online (I also checked out rondebruin website which is mentioned a lot but to no avail), but this has worked for me and I think it should work for you and anyone who will come at a later date.不幸的是,网上没有太多帮助(我还查看了 rondebruin 网站,该网站被多次提及但无济于事),但这对我有用,我认为它应该对您和以后会来的任何人有用。

To fix Excel not letting you save to your specific folder simply record a macro and do the steps you require normally.要修复 Excel 不允许您保存到特定文件夹,只需记录一个宏并执行您通常需要的步骤。 In my case they were: Save As, select PDF (in the Dialog box), Select your folder (again in the Dialog box).就我而言,它们是:另存为,选择 PDF(在对话框中),选择您的文件夹(再次在对话框中)。 Once this is done open VBA and look at the code it has just given you for that recorded Macro.完成此操作后,打开 VBA 并查看它刚刚为您提供的录制宏的代码。 Now you simply have to change the details you need, like for example the Name of the Pdf based on cell value or sheet name and that's it.现在,您只需更改所需的详细信息,例如基于单元格值或工作表名称的 Pdf 名称,仅此而已。 Once you execute your macro, macOS will ask you to grant permission to excel which you simply accept and best of all is you only have to do it once and then it works automatically.执行宏后,macOS 会要求您授予 Excel 权限,您只需接受该权限,最重要的是您只需执行一次,然后它就会自动运行。

Here's a snippet of the code I used which works on macOS Big Sur Version 11.6 and Excel Version 16.54:这是我使用的代码片段,适用于 macOS Big Sur 版本 11.6 和 Excel 版本 16.54:

Sub Save_Invoice_to_Folder()

ChDir _
    "/Users/Example/Downloads/"
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, filename:= _
    "/Users/Example/Downloads/" & "Invoice-" & Range("E3").Text & ".pdf" _
    , Quality:=xlQualityMinimum, IncludeDocProperties:=True, IgnorePrintAreas _
    :=False, OpenAfterPublish:=False

End Sub

Obviously replace "Example" with your username and add your folder path for your specific case.显然,将“示例”替换为您的用户名,并为您的特定案例添加文件夹路径。

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

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