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

Creating Extension - 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 forum is not for asking questions about writing your own macros.

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

Postby Hagar Delest » 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).

Code: Select all   Expand viewCollapse view
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   Expand viewCollapse view
'----------------------  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.
AOO 4.1.7 on Xubuntu 19.10 and 4.1.5 on Windows 10 (with winPenPack port).
User avatar
Hagar Delest
Moderator
 
Posts: 28675
Joined: Sun Oct 07, 2007 9:07 pm
Location: France

Change log

Postby Hagar Delest » Mon May 26, 2008 2:23 pm

After some searches, the solution to insert the chapter field was... in the Andrew Pitonyak's document (again!). So it should be fine now.
AOO 4.1.7 on Xubuntu 19.10 and 4.1.5 on Windows 10 (with winPenPack port).
User avatar
Hagar Delest
Moderator
 
Posts: 28675
Joined: Sun Oct 07, 2007 9:07 pm
Location: France

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

Postby Elchanan » Thu Nov 27, 2008 10:37 pm

Hagar,
Are you suggesting, in the change comment, that we modify the code you provided earlier in this post?
Thank you,
Elchanan
Elchanan
 
Posts: 2
Joined: Mon Nov 19, 2007 4:20 am

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

Postby Hagar Delest » Thu Nov 27, 2008 11:12 pm

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?
AOO 4.1.7 on Xubuntu 19.10 and 4.1.5 on Windows 10 (with winPenPack port).
User avatar
Hagar Delest
Moderator
 
Posts: 28675
Joined: Sun Oct 07, 2007 9:07 pm
Location: France

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

Postby Elchanan » Sat Nov 29, 2008 6:15 pm

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
Elchanan
 
Posts: 2
Joined: Mon Nov 19, 2007 4:20 am

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

Postby Hagar Delest » Sun Nov 30, 2008 4:16 pm

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.
AOO 4.1.7 on Xubuntu 19.10 and 4.1.5 on Windows 10 (with winPenPack port).
User avatar
Hagar Delest
Moderator
 
Posts: 28675
Joined: Sun Oct 07, 2007 9:07 pm
Location: France


Return to Code Snippets

Who is online

Users browsing this forum: No registered users and 3 guests