Link to home
Start Free TrialLog in
Avatar of Ginger16
Ginger16

asked on

overwrite existing file

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
Avatar of Marine
Marine

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

ASKER CERTIFIED SOLUTION
Avatar of Erick37
Erick37
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Ginger16

ASKER

Answered perfectly. Thanks.