

'// Construct the full name (path, file name and extension) PdfFileName = pdfFileName & "-" & Format(duplicateNumber, "000") Rinse and repeat.ĭo While Dir(folderPath & pdfFileName & "-" & Format(duplicateNumber, "000") & ".pdf") "" '// The file exists so append duplicateNumber to the file name and check if that also exists, if it does increment duplicateNumber and test again.

If Dir(folderPath & pdfFileName & ".pdf") "" Then PdfFileName = Sheets("Sheet1").Range("C1").Value & " - " & currentDate

Let me know if you need any help with getting it working. You can put this code in a standard module (Alt + F11 to open the editor, then Insert > Module) and either run it manually or call it from, say, a button's click() event for example. MsgBox Err.Number & ": " & Err.Description, vbCritical, "Error" 'Something has gone wrong, spit out an error messsage If you want to send it automatically replace. Display 'This will display the email so you can review it before sending. Attachments.Add Source:=folderPath & pdfFileName, Type:=xlTypePDF body = "This is a test." 'This is the text that will appear in the body of the email. CC = "Enjoy the attached: " & pdfFileName PdfFileName = Sheets("Sheet1").Range("C1").Value & ".pdf"ĪctiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, fileName:=folderPath & pdfFileName FolderPath = "C:\Users\exampleuser\emails\"
