我编写了以下函数,只需单击选项按钮即可将电子表格视为PDF。
Public Function RunPDFReport(CellRange As String) As String
Application.ScreenUpdating = False
refPositionDetails.Range("O2").Value = dsbDashboard.Range(CellRange).Value
dsbDashboard.Unprotect
On Error GoTo ErrHandler:
With refPositionDetails
.Visible = xlSheetVisible
.ExportAsFixedFormat Type:=xlTypePDF, Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, _
OpenAfterPublish:=True, from:=1, To:=3
.Visible = xlSheetVeryHidden
Application.ScreenUpdating = True
dsbDashboard.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
dsbDashboard.Activate
End
ErrHandler:
MsgBox "Report Canceled", vbCritical
End With
End Function
这很好用,它应该做的每件事都可以。除非我有一个当前打开的PDF的早期副本,程序直接进入我的错误处理程序。它应该能够仍然打开新PDF并覆盖旧PDF或作为新副本打开。
我错过了什么吗?
答案 0 :(得分:1)
你不能,如果它当前打开...但我没有在你的代码中看到文件名位置。 为了完成这项工作,你需要循环遍历名称,一旦成功到达名称就退出函数......所以这可能会有所帮助吗?
name = [whatever you want the names tobe pulled from]
Number_of_Pdfs_you_should_close = 0
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"C:\Users\tpike\Documents\" & name & ".pdf", Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
True
Number_of_PDFs_you_should_close = Number_of_PDFs_you_should_close + 1
name = name + "(" & Number_of_Pdfs_you_should_close & ")"