Problem with beginInsert (Note item not found error)

Hi there,I’m tired of trying to figure out what goes wrong with my script below so I thought I might ask for your help.

We have a database where we archive emails and it’s getting too big. So I would like to run a script on all archived emails to detach each attachment and replace it by a standard piece of text such as ‘File detached on C:\Garbage\2004-04-20\myfile.ext’.

I keep getting the error message ‘Note item not found’ every time I use the method beginInsert. I’ve tried to pass it a NotesEmbeddedObject, a notesRichTextNavigator and finally a notesRichTextRange and nothing seems to work. (note that I’ve tried the script below without the beginInsert and it works fine except that the standard piece of text is at the end and I’m not too happy about it).

Here is the code :

Set doc = dcSelected.GetFirstDocument

While Not doc Is Nothing

'Loop through selected Documents

'--------------------------------

Set body = doc.GetFirstItem(“Body”)

vrnDate = getDocumentDate(doc)

Set rtnav = body.CreateNavigator

If rtnav.FindFirstElement(RTELEM_TYPE_FILEATTACHMENT) Then

Set rtrange = body.CreateRange	

Do

  'Loop through attachments

  '-------------------------		

  rtrange.SetBegin(rtnav)

  rtrange.SetEnd(rtnav)

  Set eobjAttach = rtnav.GetElement

			

  strPath = CST_DETACH_ROOT_FOLDER + "\" + Format(vrnDate, "yyyy-mm-dd") + "\" + eobjAttach.source



  'Call body.BeginInsert(eobjAttach,True)  'raises the error Note item not found

  'Call body.BeginInsert(rtnav, True)  'raises the error Note item not found

  Call body.BeginInsert(rtrange,True)  'raises also the error Note item not found



  Call body.AppendText("File detached under " + strPath)

			

  'detaching

  Set objFile = New FileObject(strPath)

  If Not objFile.parentFolder.exist Then

    Call objFile.parentFolder.create(True)

  End If



  Call eobjAttach.ExtractFile(strPath)



  'Deleting

  Call eobjAttach.remove

			

Loop While rtnav.FindNextElement(RTELEM_TYPE_FILEATTACHMENT)

End If

Call doc.save(False, True)

Set doc = dcSelected.GetNextDocument(doc)

Wend