I want to overwrite a file if it already exists, with an
"example.txt already exists, overwrite existing file?" MsgBox prompt first. When I save an existing file now, it just names it "example2.txt". How do I do this?
Printable View
I want to overwrite a file if it already exists, with an
"example.txt already exists, overwrite existing file?" MsgBox prompt first. When I save an existing file now, it just names it "example2.txt". How do I do this?
Try the following code (you have to add Microsoft Scripting Runtime (SCRRUN.DLL) in Project|References).
Dim fs As FileSystemObject
Set fs = CreateObject("Scripting.FileSystemObject")
If fs.FileExists("c:\example1.txt") Then
If MsgBox("example.txt already exists, overwrite existing file?", vbYesNo) = vbYes Then
fs.DeleteFile "c:\example1.txt"
' Save the file using example1.txt
Else
' Ask for another name?
End If
Else
' Save the file using example1.txt
End If
Complete the code acording to your needs.