[Writer] Work-around for an in-line heading

Shared Libraries
Forum rules
For sharing working examples of macros / scripts. These can be in any script language supported by OpenOffice.org [Basic, Python, Netbean] or as source code files in Java or C# even - but requires the actual source code listing. This section is not for asking questions about writing your own macros.
Post Reply
User avatar
Hagar Delest
Moderator
Posts: 32665
Joined: Sun Oct 07, 2007 9:07 pm
Location: France

[Writer] Work-around for an in-line heading

Post by Hagar Delest »

Following that tutorial: [Tutorial] "In-line" or "run-in" headings, here is the Basic code to do it. The advantage is that the heading level can easily be modified inside the macro (see the User customization area at its beginning).

Code: Select all

Option Explicit			' http://user.services.openoffice.org/en/forum
Sub InLineHeadings 		' Hagar de l'Est from the concept by RGB
Dim oDoc as Object		' v1 - May 26, 2008
Dim oCharStyles as Object, oCharStyle as Object
Dim oParaStyles as Object, oParaStyle as Object
Dim oViewCursor as Object, oCurText as Object, oCursor as Object
Dim iLevel%				' Heading level
Dim sSuffix as String	' String after the in-line heading
Dim oChapField as object

'----------------------  User customization  ----------------------
iLevel = 2
sSuffix = ": "
'------------------------------------------------------------------

oDoc = stardesktop.currentComponent

' Create the styles if they don't exist
oCharStyles = oDoc.styleFamilies.getByName("CharacterStyles")
If not oCharStyles.hasByName("InLineHeading " & iLevel) then
	oCharStyle = oDoc.createInstance("com.sun.star.style.CharacterStyle")
	oCharStyles.insertByName("InLineHeading " & iLevel, oCharStyle)
End If

oParaStyles = oDoc.styleFamilies.getByName("ParagraphStyles")
oParaStyle = oParaStyles.getByName("Heading " & iLevel)
oParaStyle.ParaTopMargin = 0
oParaStyle.ParaBottomMargin = 0
oParaStyle.ParaKeepTogether = True
oParaStyle.CharColor = RGB(255, 255, 255)
oParaStyle.CharHeight = 8
       
' Apply the default formatting, then the Heading paragraph style and a new paragraph
oViewCursor =  oDoc.CurrentController.ViewCursor
oCurText = oViewCursor.Text
oCursor = oCurText.createTextCursorByRange(oViewCursor)
oCursor.gotoEndOfParagraph(False)
oCursor.gotoStartOfParagraph(True)
oCursor.CharStyleName = "Default"
oCursor.gotoEndOfParagraph(False)
oCursor.ParaStyleName = "Heading " & iLevel
oCurText.insertString(oCursor, chr$(13), False)
oCursor.CharStyleName = "InLineHeading " & iLevel

' Insert Chapter number field
oChapField = oDoc.createInstance("com.sun.star.text.TextField.Chapter")
oChapField.Level = iLevel - 1
oChapField.ChapterFormat = com.sun.star.text.ChapterFormat.NUMBER
oCurText.insertTextContent(oCursor, oChapField, False)

' Insert a space as separator
oCurText.insertString(oCursor," ", False)

' Insert Chapter name field
oChapField = oDoc.createInstance("com.sun.star.text.TextField.Chapter")
oChapField.Level = iLevel - 1
oChapField.ChapterFormat = com.sun.star.text.ChapterFormat.NAME
oCurText.insertTextContent(oCursor, oChapField, False)
oCurText.insertString(oCursor, sSuffix, False)
oCursor.CharStyleName = "Default"

End Sub
For the installation: [Tutorial] How to install a code snippet.

Code: Select all

'----------------------  User customization  ----------------------
iLevel = 2
sSuffix = ": "
'------------------------------------------------------------------
iLevel is for the heading level to be 'faked'
sSuffix is for the separator that will be automatically inserted after the fields

Note that the heading styles applied are the default Heading # ones, they have to be declared in the Tools>Outline Numbering dialog to appear in the TOC.
LibreOffice 7.6.2.1 on Xubuntu 23.10 and 7.6.4.1 portable on Windows 10
User avatar
Hagar Delest
Moderator
Posts: 32665
Joined: Sun Oct 07, 2007 9:07 pm
Location: France

Change log

Post by Hagar Delest »

After some searches, the solution to insert the chapter field was... in the Andrew Pitonyak's document (again!). So it should be fine now.
LibreOffice 7.6.2.1 on Xubuntu 23.10 and 7.6.4.1 portable on Windows 10
Elchanan
Posts: 2
Joined: Mon Nov 19, 2007 4:20 am

Re: [Writer] Work-around for an in-line heading

Post by Elchanan »

Hagar,
Are you suggesting, in the change comment, that we modify the code you provided earlier in this post?
Thank you,
Elchanan
User avatar
Hagar Delest
Moderator
Posts: 32665
Joined: Sun Oct 07, 2007 9:07 pm
Location: France

Re: [Writer] Work-around for an in-line heading

Post by Hagar Delest »

Do you mean the User customization section? If so, yes, you can change it if you want to change the level to be modified. Else, can you explain what you mean?
LibreOffice 7.6.2.1 on Xubuntu 23.10 and 7.6.4.1 portable on Windows 10
Elchanan
Posts: 2
Joined: Mon Nov 19, 2007 4:20 am

Re: [Writer] Work-around for an in-line heading

Post by Elchanan »

Thank you kindly for responding. I'm not entirely certain what questions to ask, as I've never done this before. I'll attempt as follows:

1. Your code contains "oDoc = stardesktop.currentComponent". We are using standard OO3, not StarOffice. Do we therefore need to change this line in the code, and if so, to what? (Running on Win XP Pro.)

2. If we choose to create the char and para styles manually, should we eliminate (comment out) those lines of code beginning with "oCharStyles" and "oParaStyles"? Or is a portion of this code needed anyway?

3. In the section "' Apply the default formatting, then the Heading paragraph style and a new paragraph", are the ParaStyleName and CharStyleName the only lines we need to modify?

That's a beginning. We appreciate your help!

Thank you kindly,
Elchanan
User avatar
Hagar Delest
Moderator
Posts: 32665
Joined: Sun Oct 07, 2007 9:07 pm
Location: France

Re: [Writer] Work-around for an in-line heading

Post by Hagar Delest »

1. It's standard command for OOo also.
2. You need to remove the blocks creating them, not the lines that apply them.
3. Yes, but it depends on what you want to do.

Basically, you should read some documentation about macros and understand how it works, then you'll see rather quickly what has to be modified depending on your context.
LibreOffice 7.6.2.1 on Xubuntu 23.10 and 7.6.4.1 portable on Windows 10
Post Reply