Text colour resets unexpectedly in macro

This fragment of basic macro sets the text colour to blue, writes “hello” at the text cursor, then writes “world”. If there is a newline (sCR) at the end of the first, or the start of the second output, the colour reverts to black. Is this expected behaviour, or a bug?

oTCurs.charColor = &H0000ff
otext.insertstring(oTCurs, " hello", False)
otext.insertstring(oTCurs, sCR & " world" & sCR, False)

LO 7.3.5.2 Ubuntu 22.04

Remove tag common as it contradicts tag writer. The former has wider scope than the latter which restricts the problem to Writer only (not all applications of the suite).

Your macro creates direct formatting. Refrain from doing that. You should instead apply some character style. The newline probably ends the span of the direct formatting and you revert to the state of the paragraph style.

1 Like

Yes, this extract is producing a writer file, but the macro is actually a routine in a base program.

Base has a report generator where you can define a kind of Writer template. If designed correctly, you’d probably won’t need macros any longer.

That would be nice, but you haven’t seen my output! A 2 page report in full colour with several graphs created with gnuplot. Not a task for the report builder!

Then I think macros are equally not the right tool. If you have a single page, i.e. no kind of “mail merge” where you produce several different sheets based on different DB records, you could export your various “components” to ordinary files and use a standard template to import these ordinary files where they belong in the document.

However, I feel this is not a standard document processing task (where you spread a text flow on several pages) but rather a desktop publishing job where you have one page (no overflow allowed) with several “boxes” containing “independent” objects. So, maybe an application like Scribus (free) or Quark XPress (commercial) would be a better tool.

Well it’s actually working perfectly with macros in base which export to a writer document, incorporating gnuplot graphs. So no need to reinvent the wheel.
The application is a database which stores results of a counselling process known as the Heimler Scale of Social Functioning. An interview process is conducted using a series of base forms. Both the input process and the output process are quite complicated.
I only meant to ask a simple question about text colour! I guess you have answered that - the text attribute is reset with the new paragraph. So thank you for that help and your continued interest.

The code snippet you (@Pansmanser) gave simply doesn’t show the context needed to find thinkable reasons of the failure reported as “color resets unexpectedly”. In fact the error should be supposed to be independent of whether the text attribute was applied directly or using a character style.
The attached ecxample demonstrates how I would approach the goal (preferring a style, however).
disask80945verySpecialInsertionOfAttributedText.odt (40.5 KB)

Thanks, @Lupp. I think my snippet show as much context as is needed. In practice, I have just duplicated the line
oTCurs.charColor =&H0000ff
before each output line.
Good enough for me, although I am still surprised by the behaviour I described.

Don’t worry. Wrong thinking of this kind is very popular with questioners here.
However, you might even consider if an attachment to an answer the thelper spent some time for deserves a comment on its usefulness.
In case you had attached a reduced .odt containing a piece of text, the complete code for the insertion, and showing the issue for you, I would most like have found the reason for “the behaviour” within a minute. Of course, your time surely is more valuable than mine.
I won’t annoy you another time with my attampts to help.
Hoppefully other contibutors having tried to help you are less touchy.

1 Like

Sincerely did not mean to give offence. Thank you for taking the time.