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

  • Hi Roy


    Found a way to make it work; saving the document first allows me to close it


    Code
    Dim doc As Document
    For Each doc In Documents
    If InStr(ActiveDocument.Name, "Document") Then
    ActiveDocument.SaveAs (ActiveDocument.Name)
    ActiveDocument.Close (wdDoNotSaveChanges)
    End If
    Next doc
  • 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


    Code
    Dim 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'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


    Code
    For Each doc In Documents
    If InStr(ActiveDocument.Name, "Document") Then
    ActiveDocument.SaveAs (ActiveDocument.Name)
    ActiveDocument.Close
    End If
    Next doc
  • 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.

  • Removing the line of code you said wasn't needed "doc.saveas (doc.name)" (commented out below) does not work




    at your doc.save


    Code
    Dim 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
  • 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.

  • Hi Roy


    Apparently using "Undo" is what he had in mind:


    Code
    For Each doc In Documents
    If InStr(ActiveDocument.Name, "Document") Then
    ActiveDocument.Undo (3)
    ActiveDocument.Close
    End If
    Next doc
  • It looks from the screenshots like there's a DMS (iMANAGE) involved which is what is preventing the code from closing without saving.

    Rory
    Theory is when you know something, but it doesn’t work. Practice is when something works, but you don’t know why. Programmers combine theory and practice: nothing works and they don’t know why

  • 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

  • You could try adding:


    Code
    application.enableevents = false


    before the line to close the document, then:


    Code
    application.enableevents = true


    after it. That may stop the DMS from triggering.

    Rory
    Theory is when you know something, but it doesn’t work. Practice is when something works, but you don’t know why. Programmers combine theory and practice: nothing works and they don’t know why

  • 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?

    Rory
    Theory is when you know something, but it doesn’t work. Practice is when something works, but you don’t know why. Programmers combine theory and practice: nothing works and they don’t know why

  • 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!