Insert text in the body before sending an email

I need to automatically insert text in the body content before sending an email from the mail database.

I tried this code from the "Querysend" event in the "Memo" form :

Sub Querysend(Source As Notesuidocument, Continue As Variant)

Dim doc as NotesDocument
Dim rtitem As NotesRichTextItem
Dim rtnav As NotesRichTextNavigator
Dim content As String

Call Source.Refresh(True)
Set doc = Source.Document
Set rtitem = doc.GetFirstItem(“Body”)
content = rtitem.GetUnformattedText()
Msgbox content 'Body text
Set rtnav = rtitem.CreateNavigator
Call rtnav.FindFirstElement(RTELEM_TYPE_TEXTPARAGRAPH)
Call rtitem.BeginInsert(rtnav)
Call rtitem.AddNewLine(1)
Call rtitem.AppendText(“Inserted text”)
Call rtitem.AddNewLine(1)
Call rtitem.EndInsert
content = rtitem.GetUnformattedText()
Msgbox content 'Inserted text Body text

End Sub

Unfortunately, the message on arrival does not contain the inserted text. I also noticed that any modification of the body content is not retained with this method.

Do you have another way to suggest to me ?

Thank you for your help !

Hello Fred --

There are a number of ways to achieve this. Accessing the backend document will not work for new documents though. Try adding something like this to Querysend:

	Dim ws As New NotesUIWorkspace
	Dim uidoc As NotesUIDocument
Set uidoc = ws.CurrentDocument
Call uidoc.GotoField("Body")
Call uidoc.GotoTop
Call uidoc.InsertText("Insert text here...")</code></pre>

Richard

It is because you change the backend document but the frontend document will be sent.

Rich Text Items are not automatically refreshed in frontend.

So you can use frontend functions to add the text (NotesUIDocument.InsertText) or you have to save, close and reopen the document after changing the rich text item in backend

Hi Fred,

What is happening here is that you are getting and writing data to the back end document. When we are dealing with RichText fields via our front end UI classes we run into the issue that there is no way to refresh RichText fields in the front end after making a change in the back end. So when we make changes to RTFs in the back end document, they get over written by whats in the field in the Front end.

When doing something like this we would need to save the document to get the UI changes to the back end, add the data to the RTF field and save the document again and then reload the front end document

If we are just wanting to add text, it may be easier to add a text field right below the body and the use SourceFieldSetText to add the text to that field

Thank you all for your help, i understand better why my code didn't run.

I tried to save, close and reopen the document after changing the rich text item in backend, but with no effect in a "QuerySend" context.

Better, easy and simple way is :

Call uidoc.GotoField("Body")
Call uidoc.InsertText("Insert text here...")

That's run, but it is possible to insert formatted text like color font or carriage return ?

Fred --

You can add a carriage return with something like:

Call uidoc.FieldSetText("Body", "Insert text here..." & Chr(13) & Chr(10))

However, to format color, font, etc. you'll need to first to save the document (as others have mentioned) and then use RichTextItem classes. You may also be able to use a pre-formatted subform but that'll depend on the details of your requirements.