Hi All,
I have a button to email a PDF report.
As you can see the VBA saves the PDF on a TEMP folder, in order to attach it on an email afterwards.
I thought that the last line of the code "Kill FilePath" would delete the folder inside the TEMP folder, but it only deletes the PDF file. This gives me an error when I try to export the file a second time "The file already exists"
I need one of the following solutions:
*Delete the TEMP folder every time
*Delete the folder inside the TEMP folder
*If the folder inside TEMP exists, just continue and save the pdf in there (it gets deleted at the end with "Kill FilePath" and that´s fine)
I have a button to email a PDF report.
As you can see the VBA saves the PDF on a TEMP folder, in order to attach it on an email afterwards.
I thought that the last line of the code "Kill FilePath" would delete the folder inside the TEMP folder, but it only deletes the PDF file. This gives me an error when I try to export the file a second time "The file already exists"
I need one of the following solutions:
*Delete the TEMP folder every time
*Delete the folder inside the TEMP folder
*If the folder inside TEMP exists, just continue and save the pdf in there (it gets deleted at the end with "Kill FilePath" and that´s fine)
Code:
Private Sub PDFAuditSheet_Click()
Dim FSO As Scripting.FileSystemObject
Set FSO = New Scripting.FileSystemObject
Dim FileName As String
Dim FilePath As String
Dim FolderName As String
Dim FP As String
Dim oOutlook As Outlook.Application
Dim oEmailItem As MailItem
FolderName = Me.BCaseNo
FileName = Me.BCaseNo & "-ClaimID" & Me.ClaimID & ".pdf"
FP = "G:\Claim Audit\Audit 2017\TEMP"
If Not FSO.FolderExists(FP & "" & FolderName) Then
FSO.CreateFolder (FP & "\" & FolderName)
End If
FilePath = "G:\Claim Audit\Audit 2017\TEMP\" & FolderName & "\" & FileName
If Len(Dir(FilePath)) > 0 Then
If MsgBox("This Audit has already been saved." & vbCrLf & "Do you want to overwrite it with an updated copy?", vbYesNo, "Overwrite Existing File?") = vbYes Then
Kill FilePath
Else
Exit Sub
End If
End If
DoCmd.OutputTo acOutputReport, "qryAuditSheetOut", acFormatPDF, FilePath
If oOutlook Is Nothing Then
Set oOutlook = New Outlook.Application
End If
Set oEmailItem = oOutlook.CreateItem(olMailItem)
With oEmailItem
.To = "xx@xx.com"
.Subject = "Audit Sheet" & " " & Me.BCaseNo & "-ClaimID" & Me.ClaimID
.Attachments.Add FilePath
.Display
End With
Set oEmailItem = Nothing
Set oOutlook = Nothing
'delete Temp File
Kill FilePath
End Sub