[Writer] Work-around for an in-line heading
Posted: Mon May 26, 2008 11:44 am
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).
For the installation: [Tutorial] How to install a code snippet.
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.
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
Code: Select all
'---------------------- User customization ----------------------
iLevel = 2
sSuffix = ": "
'------------------------------------------------------------------
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.