File is attaching to document when it should be attaching to field

In the simple code below, I am tryng to attach a file to a rich text field. The file is attaching but it is being attached to the bottom of the document and not the rich text field.

I know I have done this before. What am I missing?

Dim workspace As New NotesUIWorkspace

Dim uidoc As NotesUIDocument

Dim doc As NotesDocument

Dim rtitem As NotesRichTextItem

Dim filename As Variant



Set uidoc = workspace.CurrentDocument

Set doc = uidoc.Document



Const fld ="BannerGraphic"



Set rtitem = doc.GetFirstItem(fld)	



If  rtitem Is Nothing Then

	'doc.RemoveItem("BannerGraphic")		

	Set rtitem = doc.CreateRichTextItem(fld)

Else

	If  Not ( rtitem.Type = RICHTEXT ) Then                    

		Set rtitem = doc.CreateRichTextItem(fld)

	End If

End If



filename = workspace.OpenFileDialog(False,"Banner Graphic","JPG|*.jpg|GIF|*.gif|All files|*.*")

If Isarray(filename) Then

	Call rtitem.EmbedObject ( EMBED_ATTACHMENT, "", filename(0))

End If

Subject: File is attaching to document when it should be attaching to field

You cannot (at least withoug taking great care) modify the RTF of backend documents (NotesDocument) while the frontend document (NotesUIDocument) is open in edit mode.

When the user closes the frontend document, it’s overwritting your RTF with what’s in the frontend. So the file is attached to the document, but you’ve lost the “pointer” to it in the RTF.

Subject: RE: File is attaching to document when it should be attaching to field

What you said about being “careful” made me think. I went back a took a closer look at my old code.

I added the following after I embed the file and that fixed the problem.

Call doc.Save (True, True)

	doc.SaveOptions = "0"

	Call uidoc.Close

	Call workspace.EditDocument( True, doc )

	Set doc = Nothing

	Set uidoc = workspace.CurrentDocument 

	Set doc = uidoc.Document

	doc.SaveOptions = "1"

	Call doc.Save (True, True)

Subject: But not in Profile documents!

Well works fine for a regular document but does not work for a profile document!.

I have seen this type of problem before with profile documents.

I really wish Lotus would get their act together.

Subject: RE: But not in Profile documents!

What are you seeing with profile documents? Generally, when it comes to any data that needs to update within the database “session” (while it is still open), it’s better to use a regular document, because profile documents are cached until the database is re-opened. This is by design, but does cause some issues if you’re writing to the profile document.

Except for system type data storage (saving the location of a file to be imported on schedule, names of external databases to perform lookups from, etc), I’ve gotten completely away from using profile documents. Just create an “admin” type view and use a regular document for your settings. This avoids the caching issue.