Msgbox question,delete file upon clicking OK

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
3,147
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
 

Some videos you may like

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.

ranman256

Well-known Member
Joined
Jun 17, 2014
Messages
1,842
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
3,147
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
 

Watch MrExcel Video

Forum statistics

Threads
1,089,886
Messages
5,410,995
Members
403,336
Latest member
amreeves87

This Week's Hot Topics

Top