Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Checking if file exists in VBA

My issue is with Exel 2000, more used to Access than Exel
so bear with me!

Have the following simple code to save a spreadsheet which
works fine as long as the file name being saved to doesn't
already exist.

ActiveWorkbook.SaveAs Filename:= "C:\Test.XLS", _
FileFormat:=xlNormal, Password:="",
WriteResPassword:="", _
ReadOnlyRecommended:=False, _
CreateBackup:=False

Really need to handle this gracefully and not just return
an error code to the user.

Any help would be appreaciated.

Regards

Nigel
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Checking if file exists in VBA

If you want to overwrite the existing file without prompt:

Application.DisplayAlerts = False
ActiveWorkbook.SaveAs Filename:= "C:\Test.XLS", _
FileFormat:=xlNormal, Password:="",
WriteResPassword:="", _
ReadOnlyRecommended:=False, _
CreateBackup:=False
Application.DisplayAlerts = True

or
On Error Resume Next
Kill "C:\Test.xls"
On Error goto 0
ActiveWorkbook.SaveAs Filename:= "C:\Test.XLS", _
FileFormat:=xlNormal, Password:="",
WriteResPassword:="", _
ReadOnlyRecommended:=False, _
CreateBackup:=False


Or if you want to not save if it exists
if dir("C:\Test.xls") < "" then
msgbox "File exists, quitting"
exit sub
End if
ActiveWorkbook.SaveAs Filename:= "C:\Test.XLS", _
FileFormat:=xlNormal, Password:="",
WriteResPassword:="", _
ReadOnlyRecommended:=False, _
CreateBackup:=False



--
regards,
Tom Ogilvy

"NADavies" wrote in message
...
My issue is with Exel 2000, more used to Access than Exel
so bear with me!

Have the following simple code to save a spreadsheet which
works fine as long as the file name being saved to doesn't
already exist.

ActiveWorkbook.SaveAs Filename:= "C:\Test.XLS", _
FileFormat:=xlNormal, Password:="",
WriteResPassword:="", _
ReadOnlyRecommended:=False, _
CreateBackup:=False

Really need to handle this gracefully and not just return
an error code to the user.

Any help would be appreaciated.

Regards

Nigel



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,718
Default Checking if file exists in VBA

To force an overwrite in all cases use Application.DisplayAlerts = False
before the save.

--
Jim Rech
Excel MVP


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 172
Default Checking if file exists in VBA

See John Walkenbach's FileExists function.

http://j-walk.com/ss/excel/tips/tip54.htm

HTH
Paul
--------------------------------------------------------------------------------------------------------------
Be advised to back up your WorkBook before attempting to make changes.
--------------------------------------------------------------------------------------------------------------
My issue is with Exel 2000, more used to Access than Exel
so bear with me!

Have the following simple code to save a spreadsheet which
works fine as long as the file name being saved to doesn't
already exist.

ActiveWorkbook.SaveAs Filename:= "C:\Test.XLS", _
FileFormat:=xlNormal, Password:="",
WriteResPassword:="", _
ReadOnlyRecommended:=False, _
CreateBackup:=False

Really need to handle this gracefully and not just return
an error code to the user.

Any help would be appreaciated.

Regards

Nigel


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Checking to see if a file exists. JHB Excel Discussion (Misc queries) 3 August 5th 09 03:11 PM
Checking if Sheet Exists? [email protected] Excel Discussion (Misc queries) 5 September 1st 06 03:27 PM
Checking to see that a CHART exists prior to running a simple command Anton Excel Discussion (Misc queries) 0 August 29th 06 06:30 AM
Checking to see if Folder exists Dan[_25_] Excel Programming 2 September 24th 03 02:42 AM
Checking 2 c if workbook exists DavidMc Excel Programming 4 September 5th 03 07:34 PM


All times are GMT +1. The time now is 08:10 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"