我正在尝试创建一个分配给命令按钮的宏,该命令按钮选择一个工作表的一部分,然后将工作表的该部分作为 PDF 保存到服务器上包含工作簿的同一文件夹中,同时命名 PDF 基于同一工作表中单元格的值。
Sub mac_RunErrorReportDataEntry()
'
' mac_RunErrorReportDataEntry Macro
' mac_RunErrorReportDataEntry created to automate the printing of error reports for the data entry selection by Bjorn Loney on 03-16-2023. Version 0.1 03-16-2023.
'
'
'Creating and setting the variable to adjust the size of the print area based on the number of points being monitored.
Dim varPointNumber As Range
Set varPointNumber = Range("cell_MaxNumberofPoints")
'Creating and setting the variable to save the file name based on the specific sheet that is being saved.
Dim varPDFFileName As String
varPDFFileName = Range("BO1")
'Creating and setting the variable to save the file to the file path that this particular loop check workbook is saved to.
Dim varPDFFilePath As String
varPDFFilePath = Range("BO2")
'Selecting and printing the specific areas of the sheet for the Data Entry selection.
Application.ScreenUpdating = False
On Error GoTo 0
Range("C1:S" & varPointNumber + 3).Select
On Error GoTo 0
Selection.ExportAsFixedFormat _
Type:=xlTypePDF, Filename:=varPDFFilePath & varPDFFileName & ".pdf", Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
On Error GoTo 0
Range("V2").Select
Application.ScreenUpdating = True
End Sub
解决方案尝试 1:包含错误描述的 MsgBox 确定导致错误的原因确实包含错误的任何数据。
解决方法二:录制了宏,复制文件名粘贴到另存为pdf对话框失败。 解决尝试 3:在线搜索 saveas 方法,所有方法都使用打印功能而不是 ExportAsFixedFormat。尝试了打印方法,我的代码仍然无法仅将工作表的一部分打印为 PDF。 分辨率怀疑:我怀疑错误包含在行中:“Selection.ExportAsFixedFormat _”到“On Error GoTo 0” 注1:文件名中包含日期,日期格式为“月-日-年”。 注2:文件名应类似于以下示例:22-1834 Rod 1834 03-16-2023 Error Report.
问题 1:打印方法是在 VBA 中执行此操作的唯一方法吗?
问题 2:我的代码中的哪一行导致它在没有任何特定错误消息的情况下失败?
问题 3:有没有办法修改我的代码以仅将工作表的一部分打印/另存为 PDF?