Subject: NotesEmbeddedObject.Remove and Forall. Another oddity and a workaround
Hi folks,
I was very grateful to find Mitja’s post and tried it in my application. Unfortunately it did not solve the problem; I was trying to remove file attachments from rich text fields using NotesEmbeddedObject.Remove(). Although the attachments disappeared from the rich-text field, they remained attached to the document itself. In the Notes client I could see this, because the attachments appeared in grey at the bottom of the form.
I stumbled upon a fix which I still do not quite believe; I removed ALL forall structures from the code, NOT ONLY the one in the loop that contains NotesEmbeddedObject.Remove()
In Mitja’s code there is a section that creates a list of rich-text fields, that uses a FORALL;
i% = 0
Forall IMiSItem In IMiSMigDoc.Items
If IMiSItem.Type = RICHTEXT Then
Set IMiSRTitem = IMiSMigDoc.GetfirstItem(IMiSItem.Name)
If Not Isempty(IMiSRTitem.EmbeddedObjects) Then
IMiSRTNames(i%) = Cstr(IMiSItem.Name)
i% = i% +1
End If
End If
End Forall
I replaced this with a clunkier loop that uses array subscripts and a dynamic array instead FORALL;
Dim nField As Integer
Dim aRTFields() As String
Redim aRTFields( 0 To 0 )
For nField = 0 To Ubound( doc.Items )
If doc.Items( nField ).Type <> RICHTEXT Then
' This field cannot have attachments, ignore it
Elseif Isempty( doc.Items( nField ).EmbeddedObjects ) Then
' no embedded objects in this richtext field
Else
' This rich text field has one or more embedded objects, perhaps including file attachments
' Add it to our list
If aRTFields( 0 ) <> "" Then
Redim Preserve aRTFields( 0 To ( Ubound( aRTFields ) + 1 ) )
End If
aRTFields( Ubound( aRTFields ) ) = doc.Items( nField ).Name
End If
Next
and then the loop over the rich-text items goes;
For nField = 0 To Ubound( aRTFields )
Set rtItem = Nothing
Set rtItem = doc.GetFirstItem( aRTFields( nField ) )
’ … (now get the embedded objects in rtItem and remove any that are file attachments)
Next
When the code starting working after I introduced these changes, I thought I must be hallucinating and tried removing them again. The code stopped working. I re-introduced the changes and it worked again. So I don’t THINK I am imagining this, even though it seems completely illogical. This may be a funny that only affects my Notes client (version 6.5.2) or it could be strange side-effect in my application. Nonetheless I though I would post this in case it helps a fellow sufferer.
best wishes, Ian