Results 1 to 8 of 8

Thread: EXCEL: How To? Prevent abort in SaveAs when file exists ???

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Question EXCEL: How To? Prevent abort in SaveAs when file exists ???

    Esteemed Forum Participants and Lurkers:
    ========================================
    EXCEL SaveAs Function Returns

    I'm doing a SaveAs in my EXCEL VBA program:

    aworkbook.SaveAs fileName:="TEST.xls"

    If the file already exists, a modal dialog box pops up with the message:

    A file named 'TEST.xls' already exists in this location. Do you want to replace it?

    ... and 3 buttons: Yes No Cancel

    If Yes is selected, the program continues normally.
    If No or Cancel is selected, the program aborts into MS Visual Basic with the message:

    Run-time error '1004': Method 'SaveAs' of object '_Workbook' failed

    How can I determine the returns from the No and Cancel buttons and process these returns in my program without having the program abort?

    Thank you for your gracious comments, suggestions, and assistance.
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: EXCEL: How To? Prevent abort in SaveAs when file exists ???

    You need to add an error handler and trap for 1004 error.
    VB Code:
    1. Private sub command1_click
    2. On error goto myerror
    3. activeworkbook.Save
    4. If activeworkbook.saved = true then
    5.     'blah
    6. endif
    7. exit sub
    8. myerror:
    9.     if err.number = 1004 then
    10.         'canceled
    11.     endif
    12. end sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: EXCEL: How To? Prevent abort in SaveAs when file exists ???

    RobDog:

    Thanks ...

    A significant part of the question I was asking: When the error is generated, is there any way to tell which button ... No or Cancel ... was clicked?
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: EXCEL: How To? Prevent abort in SaveAs when file exists ???

    If your saving the wb as the same name as the activeworkbook then it was in this line.
    VB Code:
    1. If activeworkbook.saved = true then
    If the .Saved property is true then it was overwritten.

    You could also just check for the file's existance before the save and handle it that way.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: EXCEL: How To? Prevent abort in SaveAs when file exists ???

    I still can't figure out why there are 2 buttons (besides the "Yes") in the popup ... "No" and "Cancel" ... and what the difference is between them. They both seem to generate the exact same error, so how does one differentiate between them if I want them to program them to have different results?

    I know how to program around this, but I am trying to figure out the buttons in this particular modal dialog box.
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: EXCEL: How To? Prevent abort in SaveAs when file exists ???

    I looked deeper into this and I dont see any way (yet) to differenciate between No and Cancel.
    I guess its best just to test for the file existnace first.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: EXCEL: How To? Prevent abort in SaveAs when file exists ???

    That makes us even! Thanks for your help. I guess I'll have to program around it for now, but it will bug me until I figure out how to do it.
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: EXCEL: How To? Prevent abort in SaveAs when file exists ???

    I know how you feel, but I think if they havent given us the ability to do it in 2003 that they probably wont anytime soon.
    In Word you can execute a dialog (not shown) and get results, but I tried in Excel but its unsupported.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

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



Click Here to Expand Forum to Full Width