Variant does not contain an object

I added a button to the mail template to enable my users to report spam emails. When they click the button, it should collect the mail header information, copy the mail body, and delete the email. This works if the user fully open the email by double clicking it. However, if the user view the email in the preview pane, it deletes the email, go to the next email and the user gets this error message

Variant does not contain an object

Can anyone help me to solve this issue?

The script is as follows:

Sub Click(Source As Button)

On Error Goto errh

Dim session As New NotesSession

Dim ws As New NotesUIWorkspace

Dim db As NotesDatabase

Dim doc As NotesDocument

Dim uidoc As NotesUIDocument

Dim MailDoc As NotesDocument

Dim count As Integer

Dim AllRec As String

Dim RTItem As NotesRichTextItem

Dim MailBody As Variant

Dim choice As Integer



'Ask for confirmation

choice = Messagebox ("Are you sure you want to report this email as spam?", 36, "Report Spam")

If choice = 7 Then Exit Sub



Set db = session.CurrentDatabase

Set uidoc = ws.CurrentDocument	

Set doc = uidoc.Document

Set MailBody = doc.GetFirstItem("Body")

receivedItemText = doc.GetReceivedItemText()

If receivedItemText(0) = "" Then

	Messagebox "No Received items in document",, "No Received items"

Else

	count = 0

	Forall received In receivedItemText

		count = count + 1

		AllRec = AllRec & Chr(13) & received

	End Forall

End If

'Composing email

Set MailDoc = db.CreateDocument()

MailDoc.Form = "Memo"

MailDoc.SendTo = "spam@webroot.com" 		'put the recipient address here

MailDoc.Subject = "Reporting Spam"

Set RTItem = New NotesRichTextItem(MailDoc, "Body")

Call RTItem.AppendText(AllRec)

Call RTItem.AddNewLine(1)

Call RTItem.AppendText("To: " & doc.SendTo(0))

Call RTItem.AddNewLine(1)

Call RTItem.AppendText("Subject: " & doc.Subject(0))	

Call RTItem.AddNewLine(1)

Call RTItem.AppendText("Date: " & doc.DeliveredDate(0))	

Call RTItem.AddNewLine(1)

Call RTItem.AppendText("MIME-Version: " & doc.MIME_Version(0))

Call RTItem.AddNewLine(1)

Call RTItem.AppendText("From: " & doc.From(0))

Call RTItem.AddNewLine(2)

Call RTItem.AppendText("Original email:")

Call RTItem.AddNewLine(2)



'Append the original email

Set MailBody = doc.GetFirstItem("Body")

Call RTItem.AppendRTItem(MailBody)



'Sending email

Call MailDoc.Send(False)

Messagebox "This spam mail has been reported to webroot.", 64, "Report Spam"

Call uidoc.DeleteDocument



Exit Sub

errh:

Messagebox Error(),, "Error # " & Err()

If Err() = lsERR_NOTES_INVALID_RECEIVEDITEM Then

	receivedItemText = doc.GetItemValue("Received")

	count = 0

	Forall received In receivedItemText

		count = count + 1

		Messagebox received,, "Received item " & count

	End Forall

End If

Exit Sub

End Sub

Subject: You only have one thing Dim’d as Variant

Change the line Dim MailBody As Variant to Dim MailBody As NotesRichTextItem.

Subject: error message needs a line number

to make debugging easier