+ Reply to Thread
Results 1 to 9 of 9

Error 1004 "Document not saved" when Closing File WITHOUT CHANGES??

  1. #1
    Forum Contributor
    Join Date
    03-26-2015
    Location
    Cookeville, TN
    MS-Off Ver
    Excel 365
    Posts
    202

    Error 1004 "Document not saved" when Closing File WITHOUT CHANGES??

    A client of mine encountered this runtime error 1004 "Document not saved" today (while running a macro that opens, copies data from, and then closes [WITHOUT changes]) a series of files from a selected folder.
    I am clueless as to how a Workbook.Close command (with SaveChanges set to FALSE!) can possibly cause this error. Any ideas?

    Error 1004 Image.png

    Error 1004 Debug Image.png

  2. #2
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: Error 1004 "Document not saved" when Closing File WITHOUT CHANGES??


    Following the code in step-by-step mode, check the variable …

  3. #3
    Forum Contributor
    Join Date
    03-26-2015
    Location
    Cookeville, TN
    MS-Off Ver
    Excel 365
    Posts
    202

    Re: Error 1004 "Document not saved" when Closing File WITHOUT CHANGES??

    Quote Originally Posted by Marc L View Post

    Following the code in step-by-step mode, check the variable …
    Check what variable? OpenedFile? That is set to the workbook that was opened (and the macro is now trying to close).
    Regardless, why would we get a "Document not saved" error when the VBA code is NOT attempting to save anything??

  4. #4
    Forum Contributor
    Join Date
    03-26-2015
    Location
    Cookeville, TN
    MS-Off Ver
    Excel 365
    Posts
    202

    Re: Error 1004 "Document not saved" when Closing File WITHOUT CHANGES??

    I should note that the macro typically runs without issue. This was a seemingly "random" (unusual) error, but I'd still like to understand what might have triggered it.

  5. #5
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,302

    Re: Error 1004 "Document not saved" when Closing File WITHOUT CHANGES??

    Yes, Mark is referring to the OpenedFile variable. You should carefully check whether the expected workbook is assigned to the variable. It is also puzzling that the message appears when Excel's own error handling is enabled (it is only two lines down that you disable handling). But it is also possible that it has already been turned off before the line
    Please Login or Register  to view this content.
    It may be that this is a "temporary indisposition" of Excel and in a while everything will return to normal. You can also, just to be sure, add one line of code, which seems unnecessary, but it won't hurt either
    Please Login or Register  to view this content.
    Several times I have encountered strange errors generated by Excel, especially when our task is to process data in a loop.

    Artik

  6. #6
    Forum Contributor
    Join Date
    03-26-2015
    Location
    Cookeville, TN
    MS-Off Ver
    Excel 365
    Posts
    202

    Re: Error 1004 "Document not saved" when Closing File WITHOUT CHANGES??

    Thanks, Artik. Maybe it was just a fluke of some sort. But I'm still wondering why Excel would throw a "document not saved" error, when there is nothing in the code telling it to save the file!

  7. #7
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,302

    Re: Error 1004 "Document not saved" when Closing File WITHOUT CHANGES??

    Therefore, if an error occurs, you should check what the OpenedFile variable contains. And only with this knowledge can you try to make conjectures.

    In my opinion, you should write this piece of code so that when the line
    Please Login or Register  to view this content.
    is executed, its own error handling works. This could be simply ignoring errors
    Please Login or Register  to view this content.
    Then the file will probably be closed without saving and without an error message. These are just my guesses, this should be investigated in depth.

    Artik

  8. #8
    Forum Contributor
    Join Date
    03-26-2015
    Location
    Cookeville, TN
    MS-Off Ver
    Excel 365
    Posts
    202

    Re: Error 1004 "Document not saved" when Closing File WITHOUT CHANGES??

    Well, this is not a crucial issue. I'm just curious about what could have caused it.
    The value of OpenedFile should not be an issue. The code runs a loop that opens each file (setting OpenedFile=ActiveWorkbook immediately after the open event), and then closes it (via the code in question) before opening the next file.

  9. #9
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,302

    Re: Error 1004 "Document not saved" when Closing File WITHOUT CHANGES??

    Quote Originally Posted by Merf View Post
    The value of OpenedFile should not be an issue. The code runs a loop that opens each file (setting OpenedFile=ActiveWorkbook immediately after the open event), and then closes it (via the code in question) before opening the next file.
    I'm concerned that the line:
    Please Login or Register  to view this content.
    may be incorrectly assigning the workbook to a variable, especially when it happens in a loop (These are rare cases when Excel gets lost).
    I would more recommend a slightly different reference.
    Apply:
    Please Login or Register  to view this content.
    Here a reference will be made to the workbook being opened, not the active one.
    Seemingly the same thing, but may be more explicit and safer.

    Artik
    Last edited by Artik; 07-11-2023 at 06:41 PM.

+ 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. Replies: 1
    Last Post: 10-24-2014, 12:32 AM
  2. MSoffice Outlook 2003 message to be saved in C dir with the file name "From" "Sent" "Sub"
    By shailendra0509 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-09-2012, 01:32 AM
  3. Why do I get "Document Not Saved" error?
    By rkjudy in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-15-2010, 11:54 AM
  4. "Document not saved" error. Read on for details...
    By ajava in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-16-2009, 03:57 PM
  5. "Document not saved" error
    By AVR in forum Excel General
    Replies: 0
    Last Post: 07-03-2007, 08:46 AM
  6. Replies: 0
    Last Post: 08-07-2006, 01:35 AM
  7. "Document not saved" error message
    By Richard Clayton in forum Excel General
    Replies: 0
    Last Post: 03-11-2005, 01:50 PM

Tags for this Thread

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