If the folder exists, then continue (1 Viewer)

setis

Registered User.
Local time
Today, 01:23
Joined
Sep 30, 2017
Messages
127
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)

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
 

Minty

AWF VIP
Local time
Today, 09:23
Joined
Jul 26, 2013
Messages
10,368
You have set the Filepath variable to the entire path & filename.

Either keep it to just the path and add the name each time you need to refer to it, or create a third variable FileComplete which is the concatenation and use the right one where you need it.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 16:23
Joined
May 7, 2009
Messages
19,232
revised this portion

Code:
...
...

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
		Dim strFolderToDelete As String
		strFolderToDelete = Replace(FP & "\" & FolderName & "\", "\\","\")
		'delete all files in the folder
		KILL strFolderToDelete & "*.*" 
		'then, remove the folder
		RMDir strFolderToDelete
		're-create the folder
		MKDir strFolderToDelete
	ELSE
		Exit Sub
	End If
End If
 

setis

Registered User.
Local time
Today, 01:23
Joined
Sep 30, 2017
Messages
127
You have set the Filepath variable to the entire path & filename.

Either keep it to just the path and add the name each time you need to refer to it, or create a third variable FileComplete which is the concatenation and use the right one where you need it.

Thanks both

I am trying with:

Kill "G:\Audit 2017\TEMP\*.*"

But I get the error "File not found".. the subfolder and PDF are there though..
 

Users who are viewing this thread

Top Bottom