You will always have a problem when deleting a collection of documents. When a user executes a deletion agent on a collection of documents and if some of those documents were open or the user was not permitted (for any reason) to delete them, the user will get an error.
To prevent the error, you have several ways to exclude these documents, but you will always have the problem of setting the collection pointer to the correct position. By several trials, I found the most portable, handy and efficient way to avoid these problems.
Sub DeleteDocuments Dim s As New NotesSession Dim db As NotesDatabase Dim dc As NotesDocumentCollection Dim doc As NotesDocument Dim ExecludedDocs() As NotesDocument Dim x As Long, y As Long, Cond As Boolean Set db = s.CurrentDatabase Set dc = db.UnprocessedDocuments REM Using GetFirstDocument and GetNextDocument methods REM are much faster when you have high number of documents y = 0 Redim ExecludedDocs(y) Set doc = dc.GetFirstDocument For x = 1 To dc.Count Cond = True 'you set here the exclusion condition such as doc.IsUIDocOpen or Not doc.IsValid If Cond Then 'add the execluded document to an array of documents Redim Preserve ExecludedDocs(y) Set ExecludedDocs(y) = doc y = y + 1 End If 'do any required processes here ' ' 'you may not delete the document here, that makes the next doc not available. Set doc = dc.GetNextDocument(doc) Next If y > 0 Then 'we have excluded docs 'Remove them from collection Forall exdoc In ExecludedDocs Set exdoc = dc.GetDocument(exdoc) Call dc.DeleteDocument(exdoc) End Forall End If 'now remove the collection safely Print "deleting documents, please wait..." Call dc.RemoveAll(True) Print End Sub
Do you have comments on this tip? Let us know.