PDA

View Full Version : VBA - Export as PDF and overwrite existing PDFs



joshua1990
02-19-2018, 11:24 AM
Hey guys!

I am trying to implement an approach which creates an PDF in an defined folder.
The objective is to replace the existing PDF with the new one.
How is this possible?

My current approach looks like this:



Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)Worksheets("TblOne").ExportAsFixedFormat Type:=xlTypePDF FileName:="Draft.pdf" Quality:=xlQualityStandardEnd Sub



Does anyone have an idea?


Best regards
Josh

ranman256
02-19-2018, 12:22 PM
vFile = "c:\temp\Draft.pdf"
killFile vFile
Worksheets("TblOne").ExportAsFixedFormat Type:=xlTypePDF FileName:=vFile Quality:=xlQualityStandard


Public Sub KillFile(ByVal pvFile)
Dim FSO
On Error Resume Next
Set FSO = CreateObject("Scripting.FileSystemObject")
FSO.DeleteFile pvFile
Set FSO = Nothing
End Sub

joshua1990
02-19-2018, 12:34 PM
Many thanks!

But how can I implement your approach?
The whole code in "Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)"
Or are these two seperate procedures?