Copies attachments twice

Hi, I have following code that is supposed to go to the view, locate the document, get the attachment(s) from the rich text filed and copy them to a new document. It works fine if I have a single attachment in the rich text field but if I have two or more attachments then it copies them twice. So if I have 2 in the newly created document I get 4 attachments. Does any one see any problems with this code?

Dim attachmentdoc As NotesDocument

Dim itemA As NotesItem

Dim rtitem As Variant

Dim view As NotesView

Set view = db.getview("ByAttachmentTemplates")

Set attachmentdoc = view.GetFirstDocument

If Not attachmentdoc Is Nothing Then

	Set rtitem = attachmentdoc.GetFirstItem( "Body" )

	If ( rtitem.Type = RICHTEXT ) Then

	Forall o In rtitem.EmbeddedObjects

		If ( o.Type = EMBED_ATTACHMENT ) Then

		Set itemA = attachmentdoc.GetFirstItem("Body")

		Call itemA.CopyItemToDocument(newdoc,"Body")

	End If

	End Forall

	Call newdoc.computeWithForm(False, False)

	Call newdoc.Save (True, True)

End If

End If

Regards,

Armand

Subject: Sure, I see the problem. You are not just copying attachments, you …

are copying the entire Body item to the new document N times where N is the number of attachments. Since the Body item contains the Attachments, you are getting the duplicates. Note that you are also not just getting the attachments, but getting any text entered in the field as well.

	Set rtitem = attachmentdoc.GetFirstItem( "Body" )

	If ( rtitem.Type = RICHTEXT ) Then

	Forall o In rtitem.EmbeddedObjects

		If ( o.Type = EMBED_ATTACHMENT ) Then

		Set itemA = attachmentdoc.GetFirstItem("Body")

		Call itemA.CopyItemToDocument(newdoc,"Body")

	End If

	End Forall

	Call newdoc.computeWithForm(False, False)

	Call newdoc.Save (True, True)

Why not just:

	Set rtitem = attachmentdoc.GetFirstItem( "Body" )

	Call rtitem.CopyItemToDocument(newdoc,"Body")

	Call newdoc.computeWithForm(False, False)

	Call newdoc.Save (True, True)

Subject: RE: Sure, I see the problem. You are not just copying attachments, you …

I need a handle(rtitem) to the body field first time so I could validate that it is a rich text field and it has an attachment. Next time around I only copy rtitemA and not the rtitem.

I see, if I don’t use the forall loop, don’t validate the embedded objects etc… then it works fine.

Thanks.

Subject: RE: Sure, I see the problem. You are not just copying attachments, you …

“rtitem” and “itemA” are different references to the same thing, so you are copying the entire rich text field once for every attachment you find. There’s no need to do that. Don’t do the copy in the forall, set a flag instead:

Dim copyItemFlag As Boolean

copyItemFlag = False

Set rtitem = attachmentdoc.GetFirstItem( “Body” )

If ( rtitem.Type = RICHTEXT ) Then

Forall o In rtitem.EmbeddedObjects

If ( o.Type = EMBED_ATTACHMENT ) Then

copyItemFlag = True

End If

End Forall

If copyItemFlag Then

Call rtitem.CopyItemToDocument(newdoc,“Body”)

Call newdoc.computeWithForm(False, False)

Call newdoc.Save (True, True)

End If

Subject: Why bother verifying that it is a RT field and that it has attachments?

Worst case is the item is not RT (somehow? since it is coming from your form, hard to figure how it could not be) and is empty. Copying the field to the new doc will not hurt.

Subject: Thanks

thanks to all of you for responding. I am clear now.

Regards,

Armand