Microsoft Word - For Each aDoc in Documents- problem when closing a document part way through loop

Asked By John Wirth on 27-Jun-13 04:48 AM
I am looping through documents using the code below:

Sub ProcessDocs()
For Each ADoc In Documents
UserForm3.Show
UserForm3.TextBox1.Text = Documents.Count
Next ADoc
End Sub

UserForm3 is used to process the current document (edit, assess and grade). One closed it goes to the next document fine. My problem is that I have a command button that allows the user to close the current document (such as if user decides to process the document at a later stage.

My problem is that this results in not all open documents being processed. I think what is happening is that if I have 3 documents open and start the code, I process one which the code sees as 1 out of 3, then close the next one and the code sees that as being 2 out of 3, but when it gets to the next aDoc, it sees that it has processed 2 documents, but that now the count of Documents is 2, so therefore all documents have been processed. This leaves the last document unprocessed as the loop sees itself as being complete.

The example is a simplified one, as it may be up to 40 documents, with one or more documenst closed during the loop at varying points during the loop.