将动态范围打印为PDF



我正在尝试将活动范围打印到pdf。我得到一个运行时错误

我正在寻找的最终结果:

  • 设置有效范围(A1:F1 &
  • 设置导出所有列适合单个页面。
  • 用"a2"保存PDF这是报告的名称。
Sub Create_PDF()
' Create and save .pdf
Dim pdfName As String
pdfName = Range("A2").Text
FullName = "P:DEN-DeptPartner_SupportPublicGenerated LettersFee Summary Spreadsheets" & pdfName & " - " & Format(Now, "mm.dd.yyyy hh mm") & ".pdf"
'Sets the name and location for the new file
Dim myrange As String
myrange = Cells(Rows.Count, 6).End(xlUp).Address
'sets the string end for the print area
With ActiveSheet.PageSetup
.PrintArea = "$A$1:" & myrange
.Orientation = xlPortrait
.Zoom = False
.FitToPagesTall = False
.FitToPagesWide = 1
End With
'Setting the spreadsheet to print active content with columns fit to single page
If Dir(FullName) <> vbNullString Then
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:="P:DEN-DeptPartner_SupportPublicGenerated LettersFee Summary Spreadsheets" & pdfName & " - " & Format(Now, "mm.dd.yyyy hh mm") & ".pdf" _
, Quality:=xlQualityMedium, IncludeDocProperties:=False, _
IgnorePrintAreas:=False, OpenAfterPublish:=True
Else
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:="P:DEN-DeptPartner_SupportPublicGenerated LettersFee Summary Spreadsheets" & pdfName & Format(Now, "mm.dd.yyyy hh mm") & ".pdf" _
, Quality:=xlQualityMedium, IncludeDocProperties:=False, _
IgnorePrintAreas:=False, OpenAfterPublish:=True
End If
'This is where I am getting the runtime error and the file is not saving
End Sub

检查路径和/或文件名,因为问题就在那里。

检查你没有输入奇怪的字符,路径是可访问的,Excel可以在其中保存文件。

我刚刚在我的计算机上测试了你的代码在一个可访问的路径,它工作完美。但是,如果我试图将文件保存到受保护的路径(例如"C:/")我得到你同样的错误。

祝一切顺利。

相关内容

  • 没有找到相关文章

最新更新