How to set TextFrame borders at once?

In example like Apache OpenOffice Community Forum - [Solved] TextFrames in Writer - (View topic)
frame borders are set one by one :


oTxtFrame.BottomBorder = aBorder
oTxtFrame.TopBorder = aBorder
oTxtFrame.LeftBorder = aBorder
oTxtFrame.RightBorder = aBorder

any (more elegant) shorcut ?

if myObject.supportsService("com.sun.star.text.BaseFrame") then
  myObject.FrameStyleName = "my_highly_sophisticated_bordered_style"

and every single property defined in that style is applied in one step.
and anybody who wants to change something in all the highly_sophisticated_bordered frames, can change it within seconds, without writing code.

1 Like

Meanwhile I found a Basic function that returns a new document based on a specified registered template.

Function getOpenTemplate(sTemplateName$, Optional sComponentName$, Optional sTemplateRegionName$)
If isMissing(sComponentName) then sComponentName = "swriter"
If isMissing(sTemplateRegionName) then sTemplateRegionName = "My Templates"

Dim a(1) as new com.sun.star.beans.PropertyValue
a(0).Name = "TemplateName"
a(0).Value = sTemplateName
a(1).Name = "TemplateRegionName"
a(1).Value = sTemplateRegionName
REM the component name does not matter with unambigous template names
getOpenTemplate = StarDesktop.loadComponentFromURL("private:factory/"& sComponentName,"_blank",0,a())
End Function

Sure.

Sub SetAllBorders(oFrame, aBorder)
  oFrame.BottomBorder = aBorder
  oFrame.TopBorder = aBorder
  oFrame.LeftBorder = aBorder
  oFrame.RightBorder = aBorder
End Sub

...

SetAllBorders(oTxtFrame, aBorder)

With a frame style.

1 Like

any short/turnkey example with such a dynamically created style ?

Styles are about configuation rather than programming.

let’s call it dynamic configuration :wink:

(the code should work independantly from doc or templates)

Ship your extension with templates and styles. Save hundreds of lines of stupid code. Leave the formatting details up to the user.

any short/turnkey example with such a shipped style ?

https://forum.openoffice.org/en/forum/download/file.php?id=43784 is a template with Basic code which can be customized in thousands of ways. This one does not use cell styles because it does not deal with formatting. Instead of styles, the template deals with configuration data in named cells.
From the same template, you can create one document to merge business reports and another document to merge banking accounts (csv) while the user never has to open the Basic IDE.

From Apache OpenOffice Community Forum - [Calc, Basic] Format imported database data - (View topic) you may create many templates where each template applies its own specific formatting to lists ranges. You have some list, assign one cell style to the column heading and a sequence of cell styles to the individual columns and the macro applies the styles. The user configures the whole thing with cell styles and named ranges without loading the Basic IDE.

We can distribute templates with lots of styles, custom properties, named cells, bookmarks, fields and a little bit of embedded macro code which refers to well prepared, but still customizable template properties. No more hard coded document properties.

https://ask.libreoffice.org/uploads/short-url/wbNAfFClJDC4YyyONpeRDsHPpNR.odt is the documentation of another project of mine which adds parameterized tables to serial letters (i.e. invoices with tables of products, taxes, fees for each invoice). All the formatting needs to be done by the user. The user adds some custom properties and a logical form to his own template before calling the macro to fill his/her own invoice template(s) with data.

P.S. I hate fabricating oxt packages, but I’m pretty sure that they are able to dump templates to user profiles, so they are accessible from extension code.

to follow up, with the patient directions from @Villeroy
here’s a basic (working!) example, using loadStylesFromURL() :

 oDoc = ThisComponent

 Dim args(0) as new com.sun.star.beans.PropertyValue
 args(0).Name = "LoadFrameStyles"
 args(0).Value = true

  SF =  oDoc.getStyleFamilies()
  SF.loadStylesFromURL(convertToUrl("/tmp/defTextFrameStyle2.odt"), args)

  oFrame = oDoc.createInstance("com.sun.star.text.TextFrame")
  oDoc.Text.insertTextContent(oDoc.Text.createTextCursor, oFrame, false)
  
  oFrame.createTextCursor.String = "Demo " & chr(10) & "load Frame Style" & chr(13)
   
	With oFrame
	.FrameStyleName = "fsMyFrame"
 		.HoriOrientPosition  = 12500
	 	.VertOrientPosition  = 2400
	End With

defTextFrameStyle2.odt (11.1 KB)

image

(and without fsMyFrame)
image

More (somehow too many) details : LibreOffice Developer's Guide: Chapter 7 - Text Documents - The Document Foundation Wiki