Word VBA: Close a document without the save prompt showing each time (loop is being used)

  • Hello

    First time on here, so if I'm doing anything I shouldn't, please be kind and just let me know.

    I'm a trainee and in one of my exercises is the following task:

    (a) write a macro that uses a For loop to do this – if the document name begins with the word “Document” then close that document.
    (b) can you write the code so that the client is not prompted to save every time one of the documents is closed?

    I have tried doc.Close (wdDoNotSaveChanges) but this still brings up the prompt to save for each document.

    I look forward to hearing from you as I am well and truly stumped! ?(

    Many thanks


  • Welcome to the Forum. Please read the Forum Rules to understand how the Forum works and why I have added Code Tags to your post

    All VBA code posted in the forum must be wrapped in code tags, which you omitted, including single-line code snippets.Be sure to use them in future posts.

    How to use code tags

    Just highlight all of the code and press the <> in the post menu above button to add the code tags.


  • Thanks Roy, much appreciated. My work have encouraged me to use forums to get the answers I'm looking for as they're very busy and want me to be proactive.

    I've fixed the strName, I still can't get the save prompt to stop showing though.


  • Try this, SaveChanges:=False should close the document with no prompts and discard any changes.

    You don't need to set doc as you use the variable in a Loop

    Dim doc As Document
    For Each doc In Documents
    If Right(doc.Name, 8) = "Document" Then doc.Close SaveChanges:=False
    Next doc
  • Hi Roy

    I appreciate your suggestion, unfortunately I'm still prompted with the save prompt. I attach the amended code, preceded by my code to generate 3 blank docs with headers

  • You do not need the line

    Set doc = ActiveDocument

    Application.DisplayAlerts will not prevent the save prompt.

    Try changing savechanges:=False to savechanges:=True

  • Thanks Roy, I appreciate the points re ActiveDocument and Application.DisplayAlerts; these have now been removed.

    I changed the "savechanges" as recommended, unfortunately this still showed the save prompt

    Code used:

  • Thanks Roy, I've spent a couple of days trying to get it to work so don't feel bad. I truly appreciate your help though. Have a good day :thumbup:

  • This works for me

    I've tested it on a Saved Docement and a newly created, unsaved document.

  • Hi Roy

    I only just realised you messaged again, would you believe that it doesn't work for me lol.

    I copied and pasted your code and (loving how short it was, by the way!) but, nope, still got prompted.

    Cheers anyway, have a good evening

  • Hi Roy, again, many thanks for your time and solution. You will see below what is generated when I use your document/code. Creating a document and then saving it means both of our code works but not if the document is created and then closed without changes being saved.

    Please don't stress this, it's fine. Honest :)

    Have a good day

  • Doh, of course you did. I ran the code from your document with the results I gave above. Perhaps my firm's IT department have implemented some office protocol which means the code doesn't comply :|?

    You haven't failed to provide me with a solution, you can be happy and in good conscience that you solved this ^^

    Much appreciated. I'm happy for this post to be closed :)

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!