+ Reply to Thread
Results 1 to 4 of 4

.saveas issue. Overwrite existing file?

  1. #1
    Registered User
    Join Date
    03-29-2012
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    3

    .saveas issue. Overwrite existing file?

    Hi All

    I am having a minor issue with a program I am working on. The program has a "save as" option that allows the user to save the file with a file name provided by a cell in the work sheet. There are no problems as long as the file doesn't already exist. If the file exists I get the expected error message "A file named " " already exists in this location. Do you want to replace it? Yes No Cancel". Picking yes works fine. How can I use the No button to open the save as dialog and the Cancel button to close the message and not save?

    Here is the code
    Please Login or Register  to view this content.
    Last edited by arlu1201; 03-29-2012 at 06:28 AM. Reason: Use code tags in future.

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,624

    Re: .saveas issue. Overwrite existing file?

    Maybe use Dir to check if the file exists before trying to save it:

    Please Login or Register  to view this content.

    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Forum Contributor PingPing's Avatar
    Join Date
    02-19-2010
    Location
    London, England
    MS-Off Ver
    2007
    Posts
    158

    Re: .saveas issue. Overwrite existing file?

    Unfortunately, the SaveAs method of the Workbook object does not return a value that you can use during run-time. Additionally, trying to handle a "No" or "Cancel" differently by catching an exception also fails since the same Err.Number (1004) is returned by either event. So, I don't think you can use the SaveAs method to do what you want.

    Instead, You could use the GetSaveAsFilename method of the Application object. This method returns a string (the full path of the proposed file name). You could then write some code to deal with this string, eg. using the methods of the FileSystemObject from the Scripting.Runtime library ("Microsoft Scripting Runtime" in Tools > References of the VBA IDE).

  4. #4
    Registered User
    Join Date
    03-29-2012
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    3

    Re: .saveas issue. Overwrite existing file?

    Thanks Guys.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1