Mystical error

Hi!

I’m confused. When I run my program, I get an “Object variable not set” error. This error doesn’t occur if I try to debug the code and press “enter into” on every step. If I press “enter into”, the program completes just fine (well, it isn’t ready, but without errors), but every other time it halts.

I know the line(s) where the problem is, but as far as I (newbie to ls) know there shouldn’t be any problems.

Problem occurs in for loop and it goes few times over before error.

Here’s my code so far:

Sub Querydocumentdelete(Source As Notesuidatabase, Continue As Variant)

Dim session As New NotesSession

Dim dc As NotesDocumentCollection

Dim doc As NotesDocument



Set dc = source.Documents

Set session = New NotesSession

Set doc = dc.GetFirstDocument()



Dim docCount As Integer

Dim userRole As Integer

Dim author As String

Dim user As String

Dim isAuthor As Boolean



docCount = dc.Count

user = session.UserName

userRole = session.CurrentDatabase.QueryAccess(user)



If docCount =< 0 Then

	Messagebox  ("Cannot delete! No documents selected")

	Continue = False

Else

	

Dim i As Integer

i=1

For i=1 To docCount Step 1

'NEXT LINE IST WHERE THE ERROR IS “BORN”

Set doc = dc.GetNthDocument(i)

		

	isAuthor = GetIsAuthor(doc, user)

		

If isAuthor = True Then

Call dc.DeleteDocument(doc)

Elseif userRole = 6 Then

Call dc.DeleteDocument(doc)

	

Else

Messagebox (“Only author or a database manager can delete this document”)

Continue = False

		End If

		

	Next

	

End If

End Sub

-And heres the function where the actual crash happens:

Function GetIsAuthor(curdoc As NotesDocument, user As String) As Boolean

Dim i As Integer

Dim length As Integer

i = 0

'AT NEXT LINE PROGRAM CRASHES

length = Ubound(curdoc.Authors)+1

On Error Resume Next

GetIsAuthor = False

While i< length

If user = curdoc.Authors(i)

Then

GetIsAuthor = True

Exit Function

End If

i = i+1

Wend

End Function

The problem is that for some reason “doc” is not set in the Querydocumentdelete. It sets few time fine, but then in 3rd of 4th loop it doesn’t.

Any ideas? I’m out of them.

Thanks!

Subject: mystical error

The problem occurs because you’re deleting documents from the documentcollection, then still using getnthdocument. By the time you’ve got a few documents through you’ll be trying to get the 4th document of a collection that’s now only got 3 documents in it (for example).

There’s no need to delete the documents from the collection at all really, and also using getnthdocument on a collection is generally the slowest way of looping.

Just use a loop until you get to the end of the collection, like:

set doc = dc.getfirstdocument

Do until doc is nothing

'check access and stop if incorrect

set doc = dc.getnextdocument(doc)

Loop

As I say, don’t delete the document from the collection otherwise you’ll get a different error using this loop

Dan

Subject: another way

dim doc as notesdocument

dim docAux as notesdocument

set doc=dc.getfirstdocument

do while not doc is nothing

docAux=dc.getnextdocument(doc)

'work with doc

'if you want, delete doc

set doc=docAux

Loop

Subject: Thank you!

Thanks Dan and Guillermo! These really helped me, I got it fixed.

But still I wonder, why debugger let me go through the code without errors when pressing “step into” or is it “enter into”…