Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 58
Default the file already exists - do you want to replace the existing file?

I've got a VBA procedure that saves a record to disk using
Application.GetSaveAsFilename. However, if the file it's trying to save
already exists, the Windows API generates the above message, and the user's
only option is to either replace the existing file, or manually rename the
existing file in the File Save As.

What I'd really like to do is modify the code so that when it's run, it
takes care of renaming the existing file (if it exists) by adding a "1" to
the end of the filename, then saving the file in memory with the original
name, all without requiring the user to do anything. For simplicity, I'd be
happy to keep appending the character "1" to the end of any previously
renamed files in case this operation is performed multiple times, instead of
trying to increase the appended digit by 1 each time.

Is there any VBA code I can use to accomplish this?

Thanks in advance.

Paul


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 473
Default the file already exists - do you want to replace the existing file?

Paul James wrote:
I've got a VBA procedure that saves a record to disk using
Application.GetSaveAsFilename. However, if the file it's trying to save
already exists, the Windows API generates the above message, and the user's
only option is to either replace the existing file, or manually rename the
existing file in the File Save As.

Something along these lines:

Dim V
Dim stOther As String
Dim iVer As Integer
V = Application.GetSaveAsFilename(.....)
If TypeName(V)="Boolean" Then Exit Sub ' cancelled
' does it exist?
If Dir(V)<"" Then
' yes
stOther = V
' remove extension if present
If Right(LCase(V),4)=".xls" Then stOther=Left(stOther,Len(stOther)-4)
' find an unused numerical suffix to the filename
iVer = 1
Do While Dir(stOther & iVer & ".xls")<""
iVer = iVer + 1
Loop
' rename the existing file
Name V As stOther & iVer & ".xls"
End If
' save the current file
ActiveWorkbook.SaveAs V

Bill Manville
MVP - Microsoft Excel, Oxford, England
No email replies please - reply in newsgroup

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 58
Default the file already exists - do you want to replace the existing file?

Great code, Bill - works just fine. It looks like more than a little
thought went into creating it.

Thank you so much for sharing it with me and the rest of the group.


For the benefit of others who may be interested in this procedure, I wrote
the following line:

Name V As stOther & iVer & ".xls"

as

V = stOther & iVer & ".xls"

and the parameters I used for GetSaveAsFilename(.....) a

GetSaveAsFilename("MyData.xls", filefilter:="Excel Files (*.xls),
*.xls")

Paul


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 473
Default the file already exists - do you want to replace the existing file?

Paul James wrote:
For the benefit of others who may be interested in this procedure, I wrote
the following line:

Name V As stOther & iVer & ".xls"

as

V = stOther & iVer & ".xls"


OK - I thought your aim was to rename the existing file.
Your modification changes the name of the file you are saving to.

Bill Manville
MVP - Microsoft Excel, Oxford, England
No email replies please - reply in newsgroup

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 58
Default the file already exists - do you want to replace the existing file?

Actually, Bill, you had it exactly right - my preference was to rename the
existing file. But when I first tried running your original code, I got an
error message. So I rewrote it as I described in my last message, and at
least it ran, although not exactly as desired.

However, your follow up message inspired me to take another shot at getting
the "Name V As stOther . . . " line to work, and, voila!!! It works just as
you originally wrote it in your message, and it does exactly what I had
hoped to accomplish: it renames the existing file on disk, and saves the
open file with the specified file name. I don't know what I was doing the
first time to cause the error, but it works fine now.

So many thanks again.

Paul


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
User replaces existing file with blank during file open sjs Excel Discussion (Misc queries) 3 August 18th 08 06:16 PM
Importing text file, only option to edit existing file smokey99 Excel Discussion (Misc queries) 8 April 26th 06 09:08 PM
can i save an existing .xls file as a .csv file using command line craigkan Excel Discussion (Misc queries) 2 February 18th 05 02:01 PM
How do you replace an existing file, saying "yes" in a macro? Chris Excel Programming 0 November 19th 03 03:25 PM
Automatically replace existing file on save ronber[_2_] Excel Programming 1 September 25th 03 10:05 PM


All times are GMT +1. The time now is 02:47 PM.

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

About Us

"It's about Microsoft Excel"