You can add a few lines to check to see if the file exists before they save it.
Code:
Private Sub cmdSaveFile_Click()
Dim strFileName As String
Dim ans As Integer
CommonDialog1.Flags = &H2 ' Overwrite Flag
CommonDialog1.Filter = "RTF|*.rtf|Text|*.txt"
CommonDialog1.ShowSave
On Error GoTo SaveProblems
strFileName = CommonDialog1.FileName
If CommonDialog1.FilterIndex = 1 Then
CommonDialog1.DefaultExt = "rtf"
rtb.SaveFile strFileName
Else
CommonDialog1.DefaultExt = "txt"
rtb.SaveFile strFileName, rtfText
End If
Exit Sub
SaveProblems:
MsgBox "Can’t save the file, try again.", vbCritical
Exit Sub
End Sub
EDIT: Updated it. Now it's version 6 Utilizes an overwrite flag
Last edited by dglienna; January 31st, 2006 at 12:59 PM.
I am not sure if I had phrased my question properly. This is my requirement
When i click the SAve button on the form, the File Dialog opens. When the user enters a file name to be saved as, I would like to check if the file exists. And if it exists take some action.
I am not sure if I had phrased my question properly. This is my requirement
When i click the SAve button on the form, the File Dialog opens. When the user enters a file name to be saved as, I would like to check if the file exists. And if it exists take some action.
Is there a way this could be done?
thanks,
Andy
Try this
Code:
Private Sub cmdSave_Click()
Dim vbResponse As VbMsgBoxResult
With CommonDialog1
.CancelError = False
.InitDir = "d:\"
.Filter = "Text Files (*.txt)|*.txt|"
.ShowSave
End With
If Dir$(CommonDialog1.FileName) <> vbNullString Then
vbResponse = MsgBox(CommonDialog1.FileName & " currently exists. Do you want to save it anyway?", vbYesNo + vbQuestion, "Overlay existing file?")
If vbResponse = vbYes Then
Kill CommonDialog1.FileName
Open CommonDialog1.FileName For Append As #1
Print #1, Text1.Text
Close #1
End If
End If
End Sub
The above code will display the overwrite prompt MessageBox when a user selects a file that is already present in that folder.
That's even a better way. For some reason, today, the app.path wasn't needed in my if statement. I don't know why it was working yesterday, though. Very odd. I'm replacing my code with new code utilizing the flag.
The only difference from just &H2 is cosmetic, though, as it removes blank space from the bottom of the box (&H4 is also used to remove the "Open file as read only" checkbox from ShowOpen).
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.