VB6 CommonDialog saving file problem...

Posted on 2004-04-22
Medium Priority
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

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 520 total points
ID: 10895177
CommonDialog1.Filter = "Word Files (*.doc)|*.doc" 'this will force only doc files


Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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…
Suggested Courses
Course of the Month8 days, 14 hours left to enroll

764 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