QueryDocumentDelete all descendants

Some time ago I found a Querydocumentdelete script to remove children when deleting a parent.Now I need the same but for all descendants (the responses can go up to four levels deep).

If anyone has an idea, please post an example. Thanks in advance.


Sub Querydocumentdelete(Source As Notesuidatabase, Continue As Variant)

Dim delcol As NotesDocumentCollection

Dim respcol As NotesDocumentCollection

Dim deldoc As NotesDocument

Set delcol = Source.Documents

If delcol.Count > 0 Then

Set deldoc = delcol.GetFirstDocument

Do While Not(deldoc Is Nothing)

If deldoc.Responses.Count > 0 Then

Call deldoc.Responses.RemoveAll(Force)

End If

Set deldoc = delcol.GetNextDocument(deldoc)

Loop

End If

End Sub

Subject: Use Recursion…

Get doc…Call RemoveDocAndResponses(doc)

Sub RemoveDocAndResponses(d As NotesDocument)

Dim cdc As NotesDocumentCollection

Dim cdoc As NotesDocument

Set cdc = d.Responses

For i = cdc.Count To 1 Step -1

Set cdoc = cdc.GetNthDocument(i)

Call RemoveDocAndResponses(cdoc)

Next

Call d.Remove(True)

End Sub

Subject: Using Recursion…

Thanks for the directions Bill. When I implemented your solution the script dit exactly what I wanted it to do but it also generated an “Function Requires a valid ADT argument” error. I finaly got it working with the script below. Thanks for your help.

Sub Querydocumentdelete(Source As Notesuidatabase, Continue As Variant)

REM This code removes all descendants if a document is being deleted.

Dim DelCol As NotesDocumentCollection

Dim DelDoc As NotesDocument

Dim NextDoc As NotesDocument

Set DelCol = Source.Documents

If DelCol.Count > 0 Then

Set DelDoc = DelCol.GetFirstDocument

Do While Not(DelDoc Is Nothing)

 Call DeleteDescendants(DelDoc)

 Set NextDoc = DelCol.GetNextDocument(DelDoc)

 DelDoc.Remove (True)

 Set DelDoc = NextDoc

Loop

End If

End Sub

Sub DeleteDescendants (DelDoc As NotesDocument)

Dim RespCol As NotesDocumentCollection

Dim RespDoc As NotesDocument

Dim NextResp As NotesDocument

Set RespCol = DelDoc.Responses

Set RespDoc = RespCol.GetFirstDocument

Do Until RespDoc Is Nothing

Set NextResp = RespCol.GetNextDocument(RespDoc)

Call DeleteDescendants(RespDoc)

Call RespDoc.Remove(True)

Set RespDoc = NextResp

Loop

End Sub