Solved

VB6 CommonDialog saving file problem...

Posted on 2004-04-22
2
1,740 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

911 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

20 Experts available now in Live!

Get 1:1 Help Now