+ Reply to Thread
Results 1 to 5 of 5

No/Cancel save = Runtime Error 1004

Hybrid View

  1. #1
    Registered User
    Join Date
    11-01-2018
    Location
    USA
    MS-Off Ver
    2016
    Posts
    7

    No/Cancel save = Runtime Error 1004

    'Save file
    
        Dim Path As String
        Path = CreateObject("WScript.Shell").SpecialFolders("Desktop") & "\"
        ActiveWorkbook.SaveAs Path & "filename.xlsm"
        MsgBox "File filename.xlsm saved to desktop!"
    This will return a 1004 runtime error and stop if the file already exists and the and the user selects No or Cancel during the "save as".

  2. #2
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,914

    Re: No/Cancel save = Runtime Error 1004

    One way around this error.


        Dim Path As String
        Path = CreateObject("WScript.Shell").SpecialFolders("Desktop") & "\"
        If Dir(Path & "filename.xlsm") <> vbNullString Then MsgBox "file already exists": Exit Sub
        ActiveWorkbook.SaveAs Path & "filename.xlsm"
        MsgBox "File filename.xlsm saved to desktop!"
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  3. #3
    Registered User
    Join Date
    11-01-2018
    Location
    USA
    MS-Off Ver
    2016
    Posts
    7

    Re: No/Cancel save = Runtime Error 1004

    Quote Originally Posted by bakerman2 View Post
    One way around this error.


        Dim Path As String
        Path = CreateObject("WScript.Shell").SpecialFolders("Desktop") & "\"
        If Dir(Path & "filename.xlsm") <> vbNullString Then MsgBox "file already exists": Exit Sub
        ActiveWorkbook.SaveAs Path & "filename.xlsm"
        MsgBox "File filename.xlsm saved to desktop!"
    It has to complete a save or pass the No/Cancel or nasty things happen. So while that works, it effectively stops if the file exists.

  4. #4
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,643

    Re: No/Cancel save = Runtime Error 1004

    Quote Originally Posted by sgad View Post
    It has to complete a save or pass the No/Cancel or nasty things happen. So while that works, it effectively stops if the file exists.
    If you want to save the workbook anyway then
        Dim Path As String
        Path = CreateObject("WScript.Shell").SpecialFolders("Desktop") & "\"
        Application.DisplayAlerts = False
        ActiveWorkbook.SaveAs Path & "filename.xlsm"
        Application.DisplayAlerts = True
        MsgBox "File filename.xlsm saved to desktop!"

  5. #5
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,914

    Re: No/Cancel save = Runtime Error 1004

    When selecting Yes on msgbox old file will be deleted and new file is saved under the same name.

    Otherwise msgbox file wasn't saved.

        Dim Path As String
        
        Path = CreateObject("WScript.Shell").SpecialFolders("Desktop") & "\"
        If Dir(Path & "filename.xlsm") <> vbNullString Then
            If MsgBox("file already exists", vbYesNoCancel) = vbYes Then
                Kill Path & "filename.xlsm"
                ActiveWorkbook.SaveAs Path & "filename.xlsm"
                MsgBox "File filename.xlsm saved to desktop!"
            Else
                MsgBox "File isn't saved"
            End If
        End If
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Runtime error 1004 method save of object _workbook failed Help
    By RECOChris in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-18-2019, 08:33 AM
  2. Runtime error 1004 when trying to save file via VBA
    By avh in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-09-2015, 03:20 AM
  3. [SOLVED] Save as PDF, choosing path, runtime error 1004
    By mikkelsl in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-20-2014, 12:27 PM
  4. [SOLVED] runtime error 1004 method save of object _workbook failed
    By madhatter40 in forum Excel General
    Replies: 1
    Last Post: 06-22-2013, 07:30 AM
  5. [SOLVED] runtime error 1004 method save of object _workbook failed
    By madhatter40 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-20-2013, 03:37 AM
  6. SaveCopyAs Runtime Error 1004 - save a backup without altering open file
    By kamelkid2 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-27-2013, 08:11 PM
  7. Runtime error 1004/ Save As method help
    By [email protected] in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-17-2006, 12:00 AM

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