LotusScript: How to set paragraph style for first line in a rich-text field?

Using LotusScript, if I create a new document with a rich-text item, is there any way to set the paragraph style on the first line in the rich-text item?

The problem I find is that calling AppendParagraphStyle always adds a paragraph break, so calling it on a new rich-text item that has no other content results in a blank line at the start of the item, and the paragraph style is applied to the second line. I want to avoid that blank line.

Here’s some simple code that always produces this problem:

Dim s As New NotesSession

Dim db As NotesDatabase

Dim doc As NotesDocument

Dim rti As NotesRichTextItem

Dim rtps As NotesRichTextParagraphStyle



Set rtps=s.CreateRichTextParagraphStyle

rtps.SetTab RULER_ONE_CENTIMETER*8,TAB_LEFT



Set db=s.CurrentDatabase

Set doc=db.CreateDocument

doc.Form="Test"

Set rti=doc.CreateRichTextItem("Body")

rti.AppendParagraphStyle rtps

rti.AppendText "This should be the first paragraph."

doc.Save True,True

Subject: LotusScript: How to set paragraph style for first line in a rich-text field?

Haven’t tested this, but you could try …

Set richStyle = session.CreateRichTextStyle

… set style, for example

richStyle.FontSize = 12

richStyle.Bold = True

…

Call rti.AppendStyle(richStyle)

then put the rest of the body in.

Subject: Found a roundabout solution…

After much stuffing around, I have managed to implement a roundabout solution to the problem.

This method involves creating a document by importing “minimal” DXL which specifies a rich-text item with the paragraph and text styles that I want, then getting that as a NotesDocument and adding all the content.

The minimal DXL looks like this:

<?xml version='1.0' encoding='utf-8'?>

I’ve put that in a global LS variable (because I have to use it a lot) called blankDocXML. Alternately, it could be a constant, but I might move the DXL into a configuration document so it can be changed without coding.

Then I import the doc and add content with this code:

Dim importer As NotesDXLImporter

Set importer=s.CreateDXLImporter(blankDocXML,db)

importer.DocumentImportOption=DXLIMPORTOPTION_CREATE

importer.Import

newNoteId=importer.GetFirstImportedNoteId

Set doc=db.GetDocumentByID(newNoteId)

Set rti=doc.GetFirstItem(“Body”)

doc.Form=“Test”

’ Set whatever other fields are needed, then start adding content to the rich-text item…

rti.AppendText “Whatever text needs to go here.”

Figuring out this solution took me a while, but it works like a charm.

Subject: LotusScript: How to set paragraph style for first line in a rich-text field?

Very old, but still, I found a solution or rather a simple workaround for this problem.

'FIRST APPEND SOME DUMMY TEXT TO EMPTY rt

Call rt.appendtext(“^”)

Call rt.update

'NOW ADD FORMATTING FOR PARAGRAPH

rtpStyle.LeftMargin = 567 * 1.5

rtpStyle.RightMargin = 567 * 20.5

Call rt.AppendParagraphStyle(rtpStyle)

'NOW REMOVE FIRST LINE

Call rtnav.FindFirstString(“^”)

Call rtrange.SetBegin(rtnav)

Call rtrange.SetEnd(rtnav)

Call rtrange.remove

Simple, right?

I am sure there are other ways, well, this is mine…

Subject: LotusScript: How to set paragraph style for first line in a rich-text field?

Hello Scott,

Try This…

    Dim RTI As NotesRichTextItem

Dim Style As NotesRichTextStyle

    Set RTI = doc.CreateRichTextItem("Body")

    Set Style = aoSession.CreateRichTextStyle()

Style.Bold = True

Style.NotesColor = 4

Call RTI.appendStyle(loStyle)

     rti.AppendText "This should be the first paragraph."

    doc.Save True,True

Hope this helps

Best regards,

Munish Kaushik

Subject: LotusScript: How to set paragraph style for first line in a rich-text field?

Munish & Rampisad…

Thanks for replying, but your solutions only use a NotesRichTextStyle object, which can only set text style and is useless for what I want to do.

I specifically need a NotesRichTextParagraphStyle object so I can set things like alignment and tabs, and using one of those at the start of a rich-text item always gives me a blank line which I don’t want.

Subject: RE: LotusScript: How to set paragraph style for first line in a rich-text field?

The blank line is coming from your code rti.AppendParagraphStyle rtps

From Help …

By default the insertion occurs at the end of the item. You can change the insertion point with BeginInsert and EndInsert.

Subject: RE: LotusScript: How to set paragraph style for first line in a rich-text field?

Yes, I already know that the the blank line is coming from rti.AppendParagraphStyle rtps. That’s exactly the problem.I have to use AppendParagraphStyle to get the paragraph style I want, but it seems impossible for that method to set the paragraph style for the first line of a rich-text item.

I also had already read the help where it says “You can change the insertion point with BeginInsert and EndInsert.”

That doesn’t help at all.

If I start with a new rich-text item that has no content, BeginInsert breaks the code.

For example, with these lines in an agent:

Set rti=doc.CreateRichTextItem(“Body”)

Dim rtrange As NotesRichTextRange

Set rtrange=rti.CreateRange

rti.BeginInsert rtrange

rti.AppendParagraphStyle rtps

rti.EndInsert

As soon as it reaches BeginInsert, the agent stops running. There are no errors. There are no status-bar messages. It just stops, as if it had reached the end of the code.

I also tried a similar thing using a NotesRichTextNavigator instead of a NotesRichTextRange, but exactly the same problem occurs.

If I add some text to the item first, then the BeginInsert line is OK, but it doesn’t solve my original problem.

For example, with these lines in an agent:

Set rti=doc.CreateRichTextItem(“Body”)

rti.AppendText “-”

Dim rtnav As NotesRichTextNavigator

Set rtnav=rti.CreateNavigator

rtnav.FindFirstElement RTELEM_TYPE_TEXTRUN

rti.BeginInsert rtnav

rti.AppendParagraphStyle rtps

rti.EndInsert

Everything runs normally. The paragraph style gets inserted before the “-”, as instructed by BeginInsert, but it still adds a blank line before the paragraph style.

So as far as I can tell, it is absolutely impossible, using the LotusScript rich-text classes, to set the paragraph style for the first line in a rich-text item.

The best that can be done is setting the paragraph style for the second line and everything after it.

That’s just stupid.