Msgbox question,delete file upon clicking OK

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
2,609
Hi,
I have the working code in use shown below.

When i receive the message "File Already Exists On The Desktop" i click on OK then i need to go to the desktop to delete the file then go back and start again.

Can we add a line of code so when i see this message then clicking on OK deletes the file for me.
The path & file name will always be the same & supplied below for you.
C:\Users\Ian\Desktop\DISCOVERY II CODES.pdf


Code:
Private Sub CommandButton1_Click()Dim Filename As String
Filename = "C:\Users\Ian\Desktop\DISCOVERY II CODES.pdf"




    If FileExists(Filename) Then
        MsgBox "File Already Exists On The Desktop", vbCritical, "Land Rovery Discovery II Codes"
        Exit Sub
        Else
        MsgBox "File Saved Sucessfully On The Desktop", vbInformation, "Land Rovery Discovery II Codes"
    End If
       
    Sheets("DR SITE").Range("A1:K16").ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        "C:\Users\Ian\Desktop\DISCOVERY II CODES.pdf", Quality:= _
        xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False




End Sub
 

ranman256

Well-known Member
Joined
Jun 17, 2014
Messages
1,775
KillFile Filename

Code:
Public Sub KillFile(ByVal pvFile)
Dim FSO
On Error Resume Next
Set FSO = CreateObject("Scripting.FileSystemObject")
'FileReadOnly pvFile, False
FSO.DeleteFile pvFile
Set FSO = Nothing
End Sub
 

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
2,609
Hi,
Do i add this to the sheet ?

DR SITE right click paste.

If so i still see the message File already exists on the desktop
 

Forum statistics

Threads
1,077,614
Messages
5,335,253
Members
399,009
Latest member
twcaddell

Some videos you may like

This Week's Hot Topics

Top