Msgbox question,delete file upon clicking OK

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,699
Office Version
  1. 2007
Platform
  1. Windows
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
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
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
 
Upvote 0
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
 
Upvote 0
Can anybody advise where i apply the code in post 2 please
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,772
Members
452,353
Latest member
strainu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top