
Word VBA: Close a document without the save prompt showing each time (loop is being used)
-
-
-
That shouldn't be necessary. My example worked with saved and unsaved documents.
Your code is looping through each document, but I don't believe it is making each document active. I would think it should be
CodeDim doc As Document For Each doc In Documents If InStr(doc.Name, "Document") Then ''/// this is only overwriting the document doc.SaveAs (doc.Name) ''/// I would think this is all you need doc.Save doc.Close (wdDoNotSaveChanges) End If Next doc
wdDoNotSaveChanges closes the document with saving, which makes no sense because you have just saved it, so this is unnecessary
-
It would be interesting to see what the recommended answer is.
-
It's bizarre but your code kept prompting for me a document name "save as" but I did update my code to remove wdDoNotSaveChanges as you are, indeed, correct and my code still works
Removing the for loop also stopped it working.
I removed the declaration as not required
-
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.
I didn't suggest removing the Loop. What I said was the Loop isn't activating the documents, so the ActiveDocument remains the same. Take a closer look at the code that I just posted.
-
-
Hi Roy
My code did work with ActiveDocument. Your code also worked.
-
Yes, but I really don't think that your code is saving each document, or that saving the documents is necessary.
-
Removing the line of code you said wasn't needed "doc.saveas (doc.name)" (commented out below) does not work
-
SaveAs is used to save a document with a different name, yours isn't. It looks like it is saving as the same name.
As I said, it will be interesting to see what the expected answer is when you find out.
-
-
-
I'm not sure why. That will undo the last action.
Thanks for getting back.
-
It looks from the screenshots like there's a DMS (iMANAGE) involved which is what is preventing the code from closing without saving.
-
Yes there is, thank you for advising me that it was iManage which was preventing the code from closing without saving. Another NEW thing I've learnt
Many thanks
-
-
-
-
My fault - I'd forgotten how backward Word is
It doesn't give you a way to disable events.
Maybe try adding doc.Saved = True before closing?
-
doc.saved = true works if the document to be saved is empty otherwise I still need to do the "undo".
It does what I was instructed to do so many thanks for your help.
Have a good weekend (I don't work Fridays....smug, much!!)
Participate now!
Don’t have an account yet? Register yourself now and be a part of our community!