1

This piece of code was working but all of sudden when triggered by other program (ERP software) it gives a message that a file already exists and has to be overwrited.

This file should usually have the file name and saved in the same folder where this workbook is.

Can somebody check this code if there is something wrong, or how to ignore any message (if there is duplicate for instance)?

Sub ExpPDF()

'Save PDF Document

Dim PathFile As String
Dim PathArray() As String
Dim PathPDF As String
   
Application.DisplayAlerts = False
'Get file path
PathFile = Application.ThisWorkbook.FullName

'Split file path in path and file ending
PathArray() = Split(PathFile, ".")

'Creat file path with ".pdf" ending
PathPDF = PathArray(0) & ".pdf"
    
'PDF File is saved in directory of workbook
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
    PathPDF, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
    :=False, OpenAfterPublish:=True
    
'Closes Workbook after generatin PDF
ActiveWorkbook.Saved = True
Application.Quit
Application.DisplayAlerts = True

End sub
MmVv
  • 553
  • 6
  • 22
  • You have the exact file name in PathPDF, so if the desired behavior is always "overwrite the existing file" you could check if a file with that name already exists and delete it before executing ActiveSheet.ExportAsFixedFormat. – Christopher Hamkins Aug 25 '21 at 13:22
  • 1
    You can see how to do that in https://stackoverflow.com/questions/67835/deleting-a-file-in-vba – Christopher Hamkins Aug 25 '21 at 13:25
  • @cph Thanks, I have added couple of lines, need to wait till tomorrow to check it out what I will get from ERP system. – MmVv Aug 25 '21 at 14:21

0 Answers0