Problems applying and saving paragraph formats in LibreOffice Writer Basic macro

Hi,
I’m using a macro in order to format text in LibreOffice Writer. Earlier I formatted each paragraph in a number of ways using direct formats, but a while ago I’d thought I’d try using paragraph formats instead since that should be a more flexible/quicker way of doing it. As far as I can see what I’ve got should work - but it doesn’t, so it seems as if I’m missing something (or I’m doing something incorrectly). It’s also possible that the problems I’m having (formatting and saving/opening) aren’t related since they occur at different stages.

There are two parts to the formatting macro. First, I set up all of my paragraph formats using the following code:

document = ThisComponent

styleFamilies = document.StyleFamilies

rem PARAGRAPH STYLES
rem get the paragraph styles
paragraphStyles = styleFamilies.getByName("ParagraphStyles")

rem create the body paragraph styles
rem set properties for the "BODY" style
paragraphStyle = document.createInstance("com.sun.star.style.ParagraphStyle")
paragraphStyle.CharFontName = BODY_FONTNAME
paragraphStyle.CharHeight = BODY_CHARHEIGHT
paragraphStyle.ParaFirstLineIndent = BODY_FIRSTLINEINDENT
LineSpace = paragraphStyle.ParaLineSpacing
	LineSpace.height = BODY_LINESPACING15
	paragraphStyle.ParaLineSpacing = LineSpace
paragraphStyle.ParaAdjust = com.sun.star.style.ParagraphAdjust.BLOCK
paragraphStyle.ParaOrphans = BODY_ORPHANS
paragraphStyle.ParaWidows = BODY_WIDOWS
paragraphStyle.ParaIsHyphenation = True
paragraphStyle.ParaHyphenationMaxLeadingChars = BODY_HYPHENATIONMAXLEADINGCHARS
paragraphStyle.ParaHyphenationMaxTrailingChars = BODY_HYPHENATIONMAXTRAILINGCHARS
Endif
paraLocale = paragraphStyle.CharLocale
	paraLocale.Language = "sv"
	paraLocale.Country = "SE"
	paragraphStyle.CharLocale = paraLocale
paragraphStyles.insertByName("BODY",paragraphStyle)

etc.

And similarly for all of the other body and header paragraph formats. The values in ALL_CAPS are constants with different values. This works fine, and all of the character formats are inserted correctly into the document.

Once I’ve set up all the formats, I go through the document paragraph by paragraph, selecting the different paragraph formats for each paragraph depending on tags, location etc., formatting using the “BODY” paragraph format above if no other options exists. The code looks something like the following, with all of the options for the other types of paragraphs format removed:

rem step A1: find all *non-empty* lines
SearchDesc = Document.createSearchDescriptor
SearchDesc.SearchRegularExpression = True

rem find all lines
SearchDesc.SearchString = "^.*$"

rem step A3: fix all paragraphs
Found = Document.findFirst(SearchDesc)

rem set up variables
styleFamilies = document.StyleFamilies
paragraphStyles = styleFamilies.getByName("ParagraphStyles")

Do Until IsNull(Found)
	rem create a cursor
	Cursor = Document.Text.createTextCursorByRange(Found)

	Cursor.ParaStyleName = "BODY"
Loop

Problem: None of the settings beginning with “Char…” in the paragraph formats are applied, possibly because of direct formatting in my source document. I can’t do anything about that, though, since the source document is exported “as is” from my writing program.

As a workaround to fix that problem, I changed the Do loop as follows to apply the formats which weren’t applied when applying the paragraph formats:

Do Until IsNull(Found)
	rem create a cursor
	Cursor = Document.Text.createTextCursorByRange(Found)

	Cursor.ParaStyleName = "BODY"

	paragraphStyle = paragraphStyles.getByName("BODY")
	Cursor.CharFontName = paragraphStyle.CharFontName
	Cursor.CharHeight = paragraphStyle.CharHeight
	Cursor.CharLocale = paragraphStyle.CharLocale
Loop

After having done that, the formatting actually looks fine. It might be that I have to to this, given that the settings I add “manually” at this point are (possibly) formatted using direct formatting in the exported source file.

Problem: when I save and re-open the document, there are issues both with the formatting and with the saved paragraph formats:

  • there is some missing formatting, like indents. If I re-apply the paragraph format for paragraphs, this is fixed
  • even though the indentation settings for paragraphs are saved, some other paragraph formats settings are not saved, like widows/orphans, hyphenation settings etc.

Any help would be much appreciated.

Regards,

Anders

See Pitonyak’s book, chapter 9.3. Styles Module (there 9.3.1. Create character styles and 9.3.2. Create paragraph styles contain working macros for your task)

Hi,
thanks for getting back to me, and for the suggestion. I have been using both his other book (the OOME 4.0) and an older version of this book when I wrote the code for the macro - it was in fact the OOME which gave me the idea to switch to a paragraph format based formatting approach rather than my old clumsier approach.
As far as I can see, the chapter you quoted only contains code for creating paragraph formats (it seems to be the same code as in the OOME which I used as base for that part of my macro), and that’s not an issue for me - the paragraph formats are created correctly. My issue is that the paragraph formats aren’t applied correctly, and when I save and reopen the document, the formatting is lost and so are several paragraph format settings.
Section 7.12 of AndrewMacro actually contains a couple of examples on how to apply paragraph formats to paragraphs by simply changing the ParaStyleName property - which is what I do, which doesn’t seem to work.
Regards,
Anders

Hi, I tried different thing but finally realized that the problem with paragraph formats not being saved is an issue with LibreOffice and the RTF format (my source file format). If I save as ODT instead, both the formatting and the paragraph formats are saved. But, I get different results when running the formatting macro on the RTF file before saving as ODT (problem 1) than when saving it as ODT and then running the formatting macro (problem 2). (1) Some paragraphs aren’t italic, even though the paragraph format I apply to them contains italic formatting (this is inconsistent for the same type of paragraph/paragraph format). (2) Some paragraphs which were all italic are no longer italic, even though there is no setting for normal text in the paragraph formats (italic text in the middle of text looks ok). I was able to resolve problem (1) by applying character formats “manually” using the same workaround I’m using for the other “Char…” settings, but problem (2) is just weird.