尝试使用 VBA 在 Excel 中保存和打印单个 sheet

Trying to save and print a single sheet in Excel using VBA

我一直在为我的经理调查一个持续存在的问题,但我很困惑,他正试图解决这个问题,所以当最终用户单击 Excel [=26= 之一上的宏分配按钮时]s,它将单个 sheet 保存为 PDF 并打印 sheet。

我得到了最底部提供的图像所示的点,代码一直运行到 'ActiveSheet.ExportAsFixedFormat' 行。有没有人对我应该使用什么来获得解释有任何建议?

非常感谢。

这是我到目前为止的代码:

Public Sub SavePrint()
 ChDir "Path-to-the-file" 
 ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _ 
                                 "Path-to-the-file\ACT Form.pdf", Quality:=xlQualityStandard, _
                                 IncludeDocProperties:=True, _
                                 IgnorePrintAreas:=False, OpenAfterPublish:=True 
End Sub 

问候,基兰

注1: ChDir需要适当使用;更改路径后,不需要在Filename中包含它,Excel自动将文件保存在当前目录中。

注2:您需要PDF插件才能使用ExportAsFuxedFormat。为了确保你有它导航到 Excel > File > Options > Add-Ins > (at the bottom) Manage: COM Add-ins 然后找到 Adobe PDF 或您用于管理 PDF 的任何其他应用程序(例如 NitroPDFBlueBeam)并选中该框。如果你没有 Adobe PDF 那么你需要在你的机器上安装它并在 excel 中激活它(即选中复选框)在 运行 这个 macro 之前。

Public Sub SavePrint()
 ChDir "C:\"
 ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _ 
                             "ACT Form.pdf", Quality:=xlQualityStandard, _
                             IncludeDocProperties:=True, _
                             IgnorePrintAreas:=False, OpenAfterPublish:=True 
End Sub

这会将 PDF 文件保存在 C:\ 文件夹中。

阅读此 post 以了解有关 ChDir 的更多信息。

阅读 this 以深入了解 ExportAsFixedFormat

您可以了解如何将 Adobe PDF 添加到 excel here