Object variable is not set

I’m having a problem with an error message that keep coming up after the MAIL has not been located. (object variable not set.) It locate the mail fine if it’s there but given an the variable not set when it’s not locate.

please help… I have attach the code that i’m using to do this.

Sub Initialize

Dim session As New NotesSession

Dim db As NotesDatabase

Dim dateTime As NotesDateTime

Dim collection As NotesDocumentCollection

Dim doc As NotesDocument



Dim Count As Integer

Dim TicketNbr, LocateMAIL As String



Set db = session.CurrentDatabase

Set dateTime = New NotesDateTime( "" )



Set db = session.CurrentDatabase



Set view=db.GetView("SearchOpenMAIL")

Set doc = view.GetFirstDocument



LocateMAIL = "" 



Count = 0



TicketNbr = Trim(Cstr(doc.TicketNumber(0)))

Do While LocateMAIL <> Trim(Cstr(doc.TicketNumber(0)))

Count = Count + 1

Messagebox ( Trim(Cstr(doc.TicketNumber(0))))

Set doc = view.GetNextdocument(doc)

Loop

Messagebox(“Your MAIL has been found”)

End Sub

Subject: Object variable is not set

You are continuing your do while loop after the last document in the view has been reached.

When the last document has been reached Set doc = view.GetNextDocument(doc) will return nothing so you need to test for this

Do While (LocateIR <> Trim(Cstr(doc.TicketNumber(0)))) And Not doc Is Nothing

Subject: RE: Object variable is not set

hi

just to say that

Do While (LocateIR <> Trim(Cstr(doc.TicketNumber(0)))) And Not doc Is Nothing

cannot work.

Both statements are evaluated, so doc.TicketNumber will fail if doc is nothing…

:wink:

Subject: Object variable is not set

It’s probably erroring out at the TicketNbr line if there is not a doc.

After setting the doc, add

While Not doc Is Nothing

(your code)

Wend

To find out what line the code is breaking on, put on the LS Debugger (file/tools/Debug LotusScript) and run it again.

Subject: Object variable is not set

What is ‘LocateIR’ in your do loop? You have not declared it and without any value, you are comparing in the do loop. Do you have this valiable as global?Bye the way you are using this line twice

“Set db = session.CurrentDatabase” in your script. You can get rid of one.

Subject: RE: Object variable is not set

If Not doc is Nothing Then and/or While Not doc is Nothing…

might help. If you try to get a handle on the next document in view without getting the first one…object variable not set.

Also, what is LocateIR? You’ve set LocateMAIL to “”.

Subject: Object variable is not set

Ok. The LocateIR is ment to be locateMAIL … thanks for response I’m going to test it now.

Subject: Object variable is not set

I run it with the debugger on and it stop at LOOP and I have reset with the While Not doc Is Nothing and the same error still apply with object variable not set.

Subject: RE: Object variable is not set

Repost your code so we can see what you did.

Subject: RE: Object variable is not set

I would like to say thank you for your help. It worked with the error function. Thanks alot…

Subject: RE: Object variable is not set

I’m speculating here.

I’ve put TicketNbr = Trim(Cstr(doc.TicketNumber(0))) inside a Do While Not doc is Nothing

Then I’ve put:

If LocateMAIL <> Trim(Cstr(doc.TicketNumber(0))) Then

…

Dim session As New NotesSession

Dim db As NotesDatabase

Dim dateTime As NotesDateTime

Dim collection As NotesDocumentCollection

Dim doc As NotesDocument



Dim Count As Integer

Dim TicketNbr, LocateMAIL As String



Set db = session.CurrentDatabase

Set dateTime = New NotesDateTime( "" )

Set view=db.GetView("SearchOpenMAIL")

Set doc = view.GetFirstDocument



LocateMAIL = ""



Count = 0	

Do While Not doc Is Nothing

	TicketNbr = Trim(Cstr(doc.TicketNumber(0)))

	If LocateMAIL <> Trim(Cstr(doc.TicketNumber(0))) Then			

		Messagebox ( Trim(Cstr(doc.TicketNumber(0))))

	End If		

	Set doc = view.GetNextdocument(doc)

	Count = Count + 1

Loop

Messagebox("Your MAIL has been found")

Subject: RE: Object variable is not set

Maybe I should create a message error to pick up the object variable not set… just a thought .

Sub Initialize

Dim session As New NotesSession

Dim db As NotesDatabase

Dim dateTime As NotesDateTime

Dim collection As NotesDocumentCollection

Dim doc As NotesDocument



Dim Count As Integer

Dim TicketNbr, LocateMAIL As String



Set db = session.CurrentDatabase

Set dateTime = New NotesDateTime( "" )



	

Set view=db.GetView("SearchOpenMAIL")

Set doc = view.GetFirstDocument 

Count = 0		

LocateMAIL = "" 

While Not doc Is Nothing

TicketNbr = Trim(Cstr(doc.TicketNumber(0)))

Messagebox (" Current MAIL Number : " &TicketNbr)

Messagebox ("Number of MAIL searched : " & count)

Messagebox (" MAIL is assigned to : " + c.RequestAssignedTo(0))

Set doc = view.GetNextDocument(doc)

count = count + 1

Wend

Messagebox ("Total MAIL searched : " & count)

Set doc = view.GetNextdocument(doc)

Messagebox(" The MAIL can’t be found")

End Sub

Subject: RE: Object variable is not set

Where are you getting the error message? Run it in the debugger to find out.

You don’t need the 2nd Set doc = view.GetNextdocument(doc) - you’ve already taken care of all the docs in the while statement.

Subject: RE: Object variable is not set

I have debug the progam and remove the second Set doc = view.GetNextdocument(doc). Now it showing that the error message is coming from Dim session As New NotesSession.

Subject: RE: Object variable is not set

First of all, you should use error functions to help yourself next time. You can get the line # it is failing on, and save yourself some time.

Sub Initialize()

On Error Goto Errhandler

ContinueOK:

Exit Sub

ErrHandler:

Messagebox "Error " & Err & " line " & Erl & ": " & Error

Resume ContinueOK

End Sub

(more info: IBM - United States )

“Object variable not set” means you have an object that you are trying to use or access that never has been instantiated.

Set view=db.GetView(“SearchOpenMAIL”)

==> make sure view is not nothing

e.g. if (view is nothing) then …

Set doc = view.GetFirstDocument

==> was there actually docs found in this view? make sure doc is not nothing

TicketNbr = Trim(Cstr(doc.TicketNumber(0)))

==> you assume TicketNumber(0) exists on the note. But you did not check it

e.g. if (not( doc.HasItem(“TicketNumber”)) then …

Set doc = view.GetNextdocument(doc)

==> this loop does not really make sense to me. But you might want to change it to be safer, like this:

Do while (not (doc is nothing))

if {some condition} then

exit do

end if

…

Set doc = view.GetNextdocument(doc)

Loop

Messagebox(“Your MAIL has been found”)

Subject: Try this

What is ----> c.AssignedTo(0) in your line ----------------->MAIL is assigned to : " + c.RequestAssignedTo(0))

I am guessing it might be a typo which should be as in the below code.


Dim session As New NotesSession

Dim db As NotesDatabase

Dim dateTime As NotesDateTime

Dim collection As NotesDocumentCollection

Dim doc As NotesDocument

Dim Count As Integer

Dim TicketNbr, LocateMAIL As String

Set db = session.CurrentDatabase

Set view=db.GetView(“SearchOpenMAIL”)

Set doc = view.GetFirstDocument

Set dateTime = New NotesDateTime( “” )

Count = 0

LocateMAIL = “”

Do until doc Is Nothing

count = count + 1

TicketNbr = Trim$(Cstr(doc.TicketNumber(0)))

Msgbox " Current MAIL Number : " & TicketNbr

Msgbox " Number of MAIL searched : " & cstr(count)

Msgbox " MAIL is assigned to : " + trim$(doc.RequestAssignedTo(0))

Set doc = view.GetNextDocument(doc)

if doc is nothing then exit do

Loop

if count > 0 then

Msgbox Total MAIL searched : " & cstr(count)

else

Msgbox “Mail is not found”

end if

End Sub

Subject: RE: Object variable is not set

The second Set doc = view.GetNextdocument(doc) causes the object variable not set error message as by this stage doc is Nothing which is what causes the while loop to end.

All your loop is doing is converting doc.TicketNumber(0) to a string and incrementing a counter which doesn’t make sense. Also your last 2 messageboxes will display regardless.

I am guessing you want to test ticket number against something. If so try something like the following.

Add a variable

Dim found as Boolean

found = False

Change the condition of your while loop to

While Not doc is Nothing and Not found

In the loop test whatever it is you are checking for

If (WHATEVER = Trim(Cstr(doc.TicketNumber(0)))) then

found = true

end if

Then after the loop

if found then

Messagebox ("Total MAIL searched : " & count)

else

Messagebox(" The MAIL can’t be found")

end if