I am trying to send emails from agents, but i have an error always with the next document that the agent process.
For example, if there are 5 emails to send, the first is ok, but with the second the agent stops and show this error:
Objet variable not set
The code:
Sub Initialize
Dim ws As New NotesUIWorkspace
Dim ns As New NotesSession
Dim db As NotesDatabase
Dim docs As NotesDocumentCollection
Dim doc As NotesDocument
Dim body As NotesMIMEEntity
Dim maildoc As NotesDocument
Dim mh As NotesMIMEHeader
Dim stream As NotesStream
Dim formula As String
Dim header As String
Dim body2 As String
Set db = ns.CurrentDatabase
Set maildoc = db.Createdocument
'Set doc = ws.Currentdocument.Document
Set stream = ns.Createstream()
header = "Test Agent"
body2 = "Test Agent"
' Create the MIME headers
Set body = maildoc.CreateMIMEEntity
Set mh = body.CreateHeader("Subject")
' Dont convert text to rich text
ns.Convertmime = False
' Configure the mail
maildoc.Form = "Memo"
maildoc.Subject = "Test Agent"
maildoc.SendTo = "Email Destination"
maildoc.CopyTo = ""
maildoc.blindCopyTo = ""
Dim dateObj As New NotesDateTime("")
'@Adjust( fecha-hora ; años ; meses ; días ; horas ; minutos ; segundos ; [ DST ] )
formula = "(Form = ""frm-testdevelop-solicitud"") & (Estado = ""Borrador"")"
Set docs = db.Search(formula, Nothing, 0)
If docs.count > 0 Then
Print "hay " + Cstr(docs.count) + " docs"
Set doc = docs.GetFirstDocument
While Not doc Is Nothing
Call stream.Writetext(|<html lang="es">|)
Call stream.Writetext(|<head>|)
Call stream.Writetext(|<meta name="viewport" content="width=device-width, initial-scale=1.0">|)
Call stream.Writetext(|<meta http-equiv="X-UA-Compatible" content="ie=edge">|)
Call stream.Writetext(|</head>|)
Call stream.Writetext(|<body>|)
Call stream.Writetext(|<div style="background-color: #897d7d; border-radius: 0.5em; width: auto; height: 50px;">
<h1 style="color: white; text-align: center; padding: 0.1em;
font-family: Cambria, Cochin, Georgia, Times, 'Times New Roman', serif;">| & header & |</h1>
</div>|)
Call stream.Writetext(|<p>| & body2 & |</p>|)
Call stream.Writetext(|</body>|)
Call stream.Writetext(|</html>|)
Call body.SetContentFromText(stream, {text/html;charset="utf-8"}, ENC_NONE)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Here is the error in the second iterate.
' Close the stream
Call stream.Close()
' Restore conversion
ns.Convertmime = True
' Send it
Call maildoc.Send(False)
' Next document
Set doc = docs.GetNextDocument(doc)
Wend
Else
Print "No hay documentos"
End If
Exit Sub
In the second iterate the agent stop in this part of the code:
Call body.SetContentFromText(stream, {text/html;charset="utf-8"}, ENC_NONE)
I know that the error is because I am sending this email with HTML format, because if I remove the sendemail block of code, the agents runs fine.
And I have no idea what I am doing wrong, any suggest?
Thanks a lot!
On every run you add the new content to the stream. As you do not "Open" a file with your stream, the "close" in your code does not do anything.
Replace the "stream.close" with a "stream.truncate" to remove the contents. That should do the trick.