Object variable not set when running the agent

Please help. This is my code. It sometimes run and sometimes pops up with “object variable not set”. This is a agent scheduled to run on the server daily.

Sub Initialize

Dim se As New NotesSession 

Dim db As NotesDatabase 

Dim vw As NotesView 

Dim vdoc As NotesDocument 

Dim mdoc As NotesDocument 

Dim it1 As NotesItem 

Dim it2 As NotesItem 

Dim it3 As NotesItem

Dim dt1 As New  NotesDateTime (Today)

Dim dt2 As NotesDateTime 

Dim tv As Double 

Dim cont As Variant 

Set db = se.CurrentDatabase 

Set vw = db.GetView("SIActive")

Set vdoc = vw.GetFirstDocument 



While Not (vdoc Is Nothing)

’ For i = 1 To 63

	For i = 1 To 57

		Set it1 = vdoc.GetFirstItem("b1s" + Cstr(i))

		If it1.Values(0) <> "" Then

			Set it2 = vdoc.GetFirstItem("b1i" + Cstr(i))

			Set it3 = vdoc.GetFirstItem("b1st" + Cstr(i))

			If it2.Values(0) = "" And it3.Values(0)<>"N/A" Then

				Set dt2 = it1.DateTimeValue 

				tv = (dt1.TimeDifference(dt2) / 86400)

				If tv = -2 Then

					Set mdoc = New NotesDocument(db)

					Set it1 = New NotesItem(mdoc, "SendTo", "", NAMES)

					Call it1.AppendToTextList(vdoc.GetFirstItem("b1v"+Cstr(i)))

’ Call it1.AppendToTextList(vdoc.author(0))

’ Call it1.AppendToTextList(vdoc.authorb(0))

					Set rtitem = New NotesRichTextItem 	( mdoc, "Body" )

					mdoc.form = "Memo"

					mdoc.subject = "Serial introduction requires action - " + vdoc.contnum(0)

’ mdoc.body = “The above serial number is due for completion in 2 days, please action the section delegated to you.”

					Call rtitem.AppendText ( "The above serial number is due for completion in 2 days, please action the section delegated to you.")	

					Call rtitem.AddNewline(1)

					Call rtitem.AppendText ( "You can see the document by clicking on the added DocLink.")	

					Call rtitem.AddNewline(1)

					Call rtitem.AppendDocLink( vdoc, db.Title )

					Call mdoc.Send(False)

				End If

				If tv = 0 Then

					Set mdoc = New NotesDocument(db)

					Set it1 = New NotesItem(mdoc, "SendTo", "", NAMES)

					Call it1.AppendToTextList(vdoc.GetFirstItem("b1v"+Cstr(i)))

					Call it1.AppendToTextList(vdoc.author(0))

					Call it1.AppendToTextList(vdoc.authorb(0))

					Set rtitem = New NotesRichTextItem 	( mdoc, "Body" )

					mdoc.form = "Memo"

					mdoc.subject = "Serial introduction requires action - " + vdoc.contnum(0)

					Call rtitem.AppendText ( "The above serial number is due today and requires attention, please action the section delegated to you.")	

					Call rtitem.AddNewline(1)

					Call rtitem.AppendText ( "You can see the document by clicking on the added DocLink.")	

					Call rtitem.AddNewline(1)

					Call rtitem.AppendDocLink( vdoc, db.Title )

’ mdoc.body = “The above serial number is due today and requires attention, please action the section delegated to you.”

					Call mdoc.Send(False)

				End If

				If tv = 2 Then

					Set mdoc = New NotesDocument(db)

					Set it1 = New NotesItem(mdoc, "SendTo", "", NAMES)

					Call it1.AppendToTextList(vdoc.GetFirstItem("b1v"+Cstr(i)))

					Call it1.AppendToTextList(vdoc.author(0))

					Call it1.AppendToTextList(vdoc.authorb(0))

					Set rtitem = New NotesRichTextItem 	( mdoc, "Body" )

					mdoc.form = "Memo"

					mdoc.subject = "Serial introduction is overdue and requires action - " + vdoc.contnum(0)

					Call rtitem.AppendText ( "The above serial number is 2 days overdue and requires attention, please action the section delegated to you.")	

					Call rtitem.AddNewline(1)

					Call rtitem.AppendText ( "You can see the document by clicking on the added DocLink.")	

					Call rtitem.AddNewline(1)

					Call rtitem.AppendDocLink( vdoc, db.Title )

’ mdoc.body = “The above serial number is 2 days overdue and requires attention, please action the section delegated to you.”

					Call mdoc.Send(False)

				End If

				If tv = 5 Then

					Set mdoc = New NotesDocument(db)

					Set it1 = New NotesItem(mdoc, "SendTo", "", NAMES)

					Call it1.AppendToTextList(vdoc.GetFirstItem("b1v"+Cstr(i)))

					Call it1.AppendToTextList(vdoc.author(0))

					Call it1.AppendToTextList(vdoc.authorb(0))

					Call it1.AppendToTextList(vdoc.Manager(0))

					Call it1.AppendToTextList(vdoc.SE_Koordinator(0))	

					Set rtitem = New NotesRichTextItem 	( mdoc, "Body" )

					mdoc.form = "Memo"

					mdoc.subject = "Serial introduction is overdue and requires action - " + vdoc.contnum(0)

					Call rtitem.AppendText ( "The above serial number is 5 days overdue and requires attention, please action the section delegated to you.")	

					Call rtitem.AddNewline(1)

					Call rtitem.AppendText ( "You can see the document by clicking on the added DocLink.")	

					Call rtitem.AddNewline(1)

					Call rtitem.AppendDocLink( vdoc, db.Title )

’ mdoc.body = “The above serial number is 5 days overdue and requires attention, please action the section delegated to you.”

					Call mdoc.Send(False)

				End If

			End If

		End If

	Next

	cont = True

’ For i = 1 To 63

	For i = 1 To 57

		Set it1 = vdoc.GetFirstItem("b1m" + Cstr(i))

		If it1.Values(0) <> "" Then

			Set it2 = vdoc.GetFirstItem("b1i" + Cstr(i))

			Set it3 = vdoc.GetFirstItem("b1st" + Cstr(i))

			If it2.Values(0) = "" And it3.Values(0)<>"N/A" Then

’ If it2.Values(0) = “” Then

				cont = False

			End If

		End If	

	Next

	If cont Then

		Set mdoc = New NotesDocument(db)

		Set it1 = New NotesItem(mdoc, "SendTo", "", NAMES)

		Call it1.AppendToTextList(vdoc.author(0))

		Call it1.AppendToTextList(vdoc.authorb(0))

		Set rtitem = New NotesRichTextItem 	( mdoc, "Body" )

		mdoc.form = "Memo"

		mdoc.subject = "Serial introduction items complete - " + vdoc.contnum(0)

		Call rtitem.AppendText ( "All the items on the above serial number have been completed.")	

		Call rtitem.AddNewline(1)

		Call rtitem.AppendText ( "You can see the document by clicking on the added DocLink.")	

		Call rtitem.AddNewline(1)

		Call rtitem.AppendDocLink( vdoc, db.Title )

’ mdoc.body = “All the items on the above serial number have been completed.”

		Call mdoc.Send(False)

	End If		

	Set vdoc = vw.GetNextDocument(vdoc)

Wend

End Sub

Subject: object variable not set when running the agent

sorry to mention…when i do a test, i doesn’t show me any error

Subject: RE: object variable not set when running the agent

Please read “Debugging Domino Applications part 1 and part 2”. They describe the exact error you are getting and explain what causes it, and show how you can tell where it’s occurring.

Subject: RE: object variable not set when running the agent

Sounds like your problem occurs when it’s scheduled and not when your run it. I would try Dan’s suggestion but change the MsgBox to an e-mail that way when it happens you get notified and you can determine what happened.

There are lots of possible places things could go wrong in your code. For example:

Set it1 = vdoc.GetFirstItem(“b1s” + Cstr(i))

This assumes b1s[#] exist. You should check that before you try and assign it.

if vDoc.HasItem( “b1s” + CStr(i) ) then

Set it1 = vdoc.GetFirstItem("b1s" + Cstr(i))

else

' Put some code here to handle error or give you more info and add yo your e-mail

End if

Subject: object variable not set when running the agent

Where does it give you the object variable not set error?

You need to add some error handling as this is run on the server, so at the top of the agent add ‘On error goto errhandler’

At the bottom, before the End Sub add the following lines:

Exit sub

errhandler:

msgbox err & " at line " & erl & ": " & error$

End

You should then get the error message on the log giving you the error number, line number and error itself.

Then you can start working out the problem

Dan