Solved

overwrite existing file

Posted on 2000-04-10
3
222 Views
Last Modified: 2010-05-02
How can I set the default filename in the save dialog box to a varible name, such as strTitle, and ask to overwrite if the file already exists. Her is the code.
Private Sub cmdSave_Click()

'Declare variable
Dim strFilename As String
'Do the error handler
cdbOpenSave.CancelError = True
On Error GoTo ErrHandler

'Set the properties of the text control
cdbOpenSave.Filter = _
"Text Files|*.txt*"

'Show the save-as dialog box
cdbOpenSave.ShowSave

'retrieve the filename
strFilename = cdbOpenSave.FileName & ".txt"

'save the file
Open strFilename For Output As #1
Print #1, Text1
Close #1

Exit Sub
ErrHandler:
End
0
Comment
Question by:Ginger16
3 Comments
 
LVL 6

Expert Comment

by:Marine
ID: 2702712
Do you mean you already want to have something in the box there like a filename ? If so, then this would work
just assign a string with file name to a property FileName.


Dim sFile as string
Dim strFilename As String
'Do the error handler
cdbOpenSave.CancelError = True
On Error GoTo ErrHandler

'Set the properties of the text control
cdbOpenSave.Filter = _
"Text Files|*.txt*"
sFile = "SomeFile"
cdb.FileName = sFile
'Show the save-as dialog box
cdbOpenSave.ShowSave

'retrieve the filename
strFilename = cdbOpenSave.FileName & ".txt"

'save the file
Open strFilename For Output As #1
Print #1, Text1
Close #1

Exit Sub
ErrHandler

0
 
LVL 32

Accepted Solution

by:
Erick37 earned 50 total points
ID: 2702718
Option Explicit

Private Sub Command1_Click()
'Declare variable
Dim strFilename As String
Dim strTitle As String
'Do the error handler
cdbOpenSave.CancelError = True
On Error GoTo ErrHandler

'Set the properties of the text control
cdbOpenSave.Filter = "Text Files *.txt|*.txt*"
'Set default extetion
cdbOpenSave.DefaultExt = "txt"
'Set default filename
strTitle = "c:\windows\test.txt"
cdbOpenSave.filename = strTitle
'Prompt on overwrite
cdbOpenSave.Flags = cdlOFNOverwritePrompt
'Show the save-as dialog box
cdbOpenSave.ShowSave

'Get the filename
strFilename = cdbOpenSave.filename

'save the file
Open strFilename For Output As #1
Print #1, Text1.Text
Close #1

Exit Sub
ErrHandler:

End Sub
0
 

Author Comment

by:Ginger16
ID: 2702861
Answered perfectly. Thanks.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…

785 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