NotesDocument.save() causing loss of rich text formatting

583 views Asked by At

I have following code in an lotusscript agent that removes attachments from NotesDocuments. But NotesDocument.save() causes loss of rich text formatting (font, color). Is there any way to retain the formatting?

Sub removeAttachments_v2(doc As NotesDocument)
    Dim session As NotesSession
    Dim rtitem As Variant
    Dim filename As String
    Dim ans As Variant

    Set session = New NotesSession
    Dim richstyle As NotesRichTextStyle
    Set richstyle = session.CreateRichTextStyle
    richstyle.NotesColor = COLOR_BLUE

    If doc.HasEmbedded Then
        Set rtitem = doc.getfirstitem("Body")
        If (rtitem.type = RICHTEXT) Then
            ForAll object In rtitem.EmbeddedObjects
                If (object.Type = EMBED_ATTACHMENT) Then
                    filename = object.source
                    Call object.remove
                    Call rtitem.AddNewLine( 2 )
                    Call rtitem.AppendStyle(richstyle)
                    Call rtitem.AppendText( "Attachemnt removed: " & filename )
                    Call doc.Save( True, True , True )
                End If
            End ForAll
        End If
    End If
End sub

Edit1: Initialize function

Sub Initialize
    Dim db As New NotesDatabase("","")
    Dim col As NotesDocumentCollection
    Dim doc As NotesDocument

    Call db.Open("", "C:\this\is\db\dir\test.nsf")
    Set col = db.Alldocuments

    Set doc = col.Getfirstdocument()
    While Not ( doc Is Nothing )
        Call RemoveAttachments_v2(doc)
        Call doc.Save(False, False, False)
        Set doc = col.GetNextDocument( doc )
    Wend
End Sub
1

There are 1 answers

2
Tode On

Despite of the fact, that you save the document for every attachment I cannot find any reason, why this should happen. I just copied your code in an agent, and it removes the attachments as desired and appends the text in blue...

No formatting is lost...

The error has to be somewhere else in your code, probably in the calling function.

OLD ANSWER (wrong due to own tests, just kept here as history):

The Problem here most probably is: you defined rtitem as Variant. And getfirstitem gets you a NotesItem instead of a NotesRichtextItem, so when saving, it is converted to a "plain Text" item.

Most probably you used Variant instead of NotesRichtextItem, because there are Mime- mails where defining the variable as NotesRichtextItem will cause an "Type Missmatch" or similar error. As long as you do not write anything back this is OK.

As Mime Mails need complete different handling to achieve your goal, you should first fix the code for pure NotesRichtextItems by using the right type, and then write another code- branch for handling Mime- items