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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VBA error replacing data 6 40
How does CurrentUser work? 10 38
Set email body to html using vbscript 6 49
Dinamic report to Crosstab query 9 33
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

856 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