Solved

overwrite existing file

Posted on 2000-04-10
3
219 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

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…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
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…

762 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

23 Experts available now in Live!

Get 1:1 Help Now