首页 > 解决方案 > VBA Script to Create PDF from Excel Goes to Printer and Doesn't Export File

问题描述

I have the following code which worked well on a Windows machine but since I moved to a MacBook, it is causing me problems:

With ActiveSheet
  Fname = "SalarySlip-" + employeeName + "-" + month + ".pdf"

  .ExportAsFixedFormat Type:=xlTypePDF, fileName:= _
    salarySlipsFolderPath & Fname, Quality:=xlQualityStandard, _
    IncludeDocProperties:=True, IgnorePrintAreas:=False, 

  OpenAfterPublish:=False
End With

Rather than exporting as a PDF file (it worked like that on Windows), a request is sent to the printer to print the file. What am I missing here?

I am using MacOS Catalina 10.15.4 running MS Excel for MAC version 16.40.

标签: excelvbamacospdfexport

解决方案


如果您用作变量的一个单元格值包含斜杠(例如,日期month为 2020/01/01),则 PDF 文件的命名过程将失败并将其发送到打印机。我经历了几个绝望的小时......

并尝试

ChDir "/" & ActiveWorkbook.Path

Sub作为and的第一行

fileName:= _
    "/" & salarySlipsFolderPath & Fname, Quality:=xlQualityStandard, _

作为文件名。一"/"开始它对我有用。


推荐阅读