Solved

VB6 CommonDialog saving file problem...

Posted on 2004-04-22
2
1,738 Views
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 = ""
    CommonDialog1.ShowSave
    wordFileName = CommonDialog1.FileName
    MsgBox "file name is " & wordFileName

End Sub
0
Comment
Question by:barry922
2 Comments
 
LVL 7

Expert Comment

by:OHDev2004
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 = ""
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 :)
0
 
LVL 19

Accepted Solution

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

S
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

757 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now