VB6 CommonDialog saving file problem...

Posted on 2004-04-22
Last Modified: 2013-12-26
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 = ""
    wordFileName = CommonDialog1.FileName
    MsgBox "file name is " & wordFileName

End Sub
Question by:barry922

Expert Comment

ID: 10894797
Hi barry922 ,, Hope this will do the trick :)


Private Sub Form_Load()
Dim wordFileName As String

 wordFileName = ""
 CommonDialog1.Filter = "Word Files (*.doc)"
 CommonDialog1.FilterIndex = 1
 CommonDialog1.Flags = cdlOFNOverwritePrompt
 CommonDialog1.FileName = ""
 wordFileName = CommonDialog1.FileName
If Not FileExists(wordFileName) Then
'routine of saving file
MsgBox "file name is " & wordFileName
'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 :)
LVL 19

Accepted Solution

Shauli earned 130 total points
ID: 10895177
CommonDialog1.Filter = "Word Files (*.doc)|*.doc" 'this will force only doc files


Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Excel object stays open 19 76
VB 6.0 printer how to align 6 62
MsgBox 2 55
How to hault or freeze parent form when a 2d form is open in vb6 3 38
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

778 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question