GetSaveAsFilename: run-time error if not wanting to replace

  • Thread starter Thread starter e18
  • Start date Start date
E

e18

Does anyone have the answer to how I can choose NO when asked if I wan'
to replace an existing file, and not get error?

Thanks,
Erlend



fTypes = "Text files (*.txt),*.txt,All files (*.*),*.*"

fName = "C:\bbar\" & nm & ".txt"
f = Application.GetSaveAsFilename(InitialFilename:=fName
Filefilter:=fTypes, FilterIndex:=1)

If f = False Then Exit Sub

ActiveWorkbook.SaveAs Filename:=f, FileFormat:= _
xlText, CreateBackup:=Fals
 
Hi Erlend,

Try this

Dim fTypes, fName, f, nm

fTypes = "Text files (*.txt),*.txt,All files (*.*),*.*"

fName = "C:\myTest\Book1.xls"
f = Application.GetSaveAsFilename(InitialFilename:=fName,
Filefilter:=fTypes, FilterIndex:=1)

If f = False Then Exit Sub

On Error Resume Next
ActiveWorkbook.SaveAs Filename:=f, FileFormat:= _
xlText, CreateBackup:=False
On Error GoTo 0


--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)
 
Thank you very much!

'On Error' was new to me, something to learn every day.
I made the code like this, and it works just fine :


Dim fTypes, fName, f, nm

fTypes = "Text files (*.txt),*.txt,All files (*.*),*.*"

fName = "C:\myTest\Book1.xls"
TRY_AGAIN
f = Application.GetSaveAsFilename(InitialFilename:=fName,
Filefilter:=fTypes, FilterIndex:=1)

If f = False Then Exit Sub

On Error GoTo *try_again*
ActiveWorkbook.SaveAs Filename:=f, FileFormat:= _
xlText, CreateBackup:=False



However if I am too stupid and selects the same filename twice, I ge
the run-time error the second time.

Erlen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top