Solved

overwrite existing file

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying 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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
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…

839 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