barry922
asked on
VB6 CommonDialog saving file problem...
The following code is an attempt to use CommonDialog1 to save a word file. My question is -
If I save a word file named report.doc (and this file name already exists in a directory), then the CommonDialog1 will prompt a warning message about overwriting the report.doc file. But if I save a word file named report (again, the file report.doc already exists in a directory), then the CommonDialog1 will NOT prompt any warning message. As a result, the report.doc will be overwritten. How do you modify the code so that the report.doc won't be overwritten?
Private Sub Form_Load()
Dim wordFileName As String
wordFileName = ""
CommonDialog1.Filter = "Word Files (*.doc)"
CommonDialog1.FilterIndex = 1
CommonDialog1.Flags = cdlOFNOverwritePrompt
CommonDialog1.FileName = ""
CommonDialog1.ShowSave
wordFileName = CommonDialog1.FileName
MsgBox "file name is " & wordFileName
End Sub
If I save a word file named report.doc (and this file name already exists in a directory), then the CommonDialog1 will prompt a warning message about overwriting the report.doc file. But if I save a word file named report (again, the file report.doc already exists in a directory), then the CommonDialog1 will NOT prompt any warning message. As a result, the report.doc will be overwritten. How do you modify the code so that the report.doc won't be overwritten?
Private Sub Form_Load()
Dim wordFileName As String
wordFileName = ""
CommonDialog1.Filter = "Word Files (*.doc)"
CommonDialog1.FilterIndex = 1
CommonDialog1.Flags = cdlOFNOverwritePrompt
CommonDialog1.FileName = ""
CommonDialog1.ShowSave
wordFileName = CommonDialog1.FileName
MsgBox "file name is " & wordFileName
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
==========================
Private Sub Form_Load()
Dim wordFileName As String
wordFileName = ""
CommonDialog1.Filter = "Word Files (*.doc)"
CommonDialog1.FilterIndex = 1
CommonDialog1.Flags = cdlOFNOverwritePrompt
CommonDialog1.FileName = ""
Again:
CommonDialog1.ShowSave
wordFileName = CommonDialog1.FileName
If Not FileExists(wordFileName) Then
'routine of saving file
MsgBox "file name is " & wordFileName
Else
'cancel save or show the savedialog again!
MsgBox "Sorry File Already Exists ,Please verify Another Name"
Goto Again:
End If
End Sub
Private Function FileExists(ByVal strPathName As String) As Integer
Dim intFileNum As Integer
On Error Resume Next
If Right$(strPathName, 1) = "\" Then
strPathName = Left$(strPathName, Len(strPathName) - 1)
End If
intFileNum = FreeFile
Open strPathName For Input As intFileNum
FileExists = IIf(Err, False, True)
Close intFileNum
Err = 0
End Function
==========================
If you need any help ,, ask it :)