how to create and fill a table of contents ?

Andrew Douglas Pitonyak andrew at pitonyak.org
Sat Feb 9 13:24:49 UTC 2019


 From OOME on my web site, well, part of that section:

Finding and inserting a table of contents (TOC) is easy unless you want 
to change the defaults. The following macro checks ThisComponent to see 
if the document contains a content index.

REM Find TOC if it exists.

oIndexes = ThisComponent.getDocumentIndexes()

bIndexFound = False

For i = 0 To oIndexes.getCount() - 1

oIndex = oIndexes.getByIndex(i)

If oIndex.supportsService("com.sun.star.text.ContentIndex") Then

bIndexFound = True

Exit For

End If

Next

Use dispose to remove an existing index from the document.

When I create a TOC, I usually set CreateFromOutline to true to create 
the index based on the document outline styles (see Listing 399).

/*Listing 399.* Insert a standard TOC into a document./

Sub InsertATOC

REM Author: Andrew Pitonyak

Dim oCurs 'Used to insert the text content.

Dim oIndexes 'All of the existing indexes

Dim oIndex 'TOC if it exists and a new one if not

Dim i% 'Find an existing TOC

Dim bIndexFound As Boolean 'Flag to track if the TOC was found





REM Find TOC if it exists.

oIndexes = ThisComponent.getDocumentIndexes()

bIndexFound = False

For i = 0 To oIndexes.getCount() - 1

oIndex = oIndexes.getByIndex(i)

If oIndex.supportsService("com.sun.star.text.ContentIndex") Then

bIndexFound = True

Exit For

End If

Next

If Not bIndexFound Then

Print "I did not find an existing content index"

REM Create and insert a new TOC.

REM The new TOC must be created by the document that will contain the TOC.

oIndex = ThisComponent.createInstance("com.sun.star.text.ContentIndex")



oIndex.CreateFromOutline = True

oCurs = ThisComponent.getText().createTextCursor()

oCurs.gotoStart(False)

ThisComponent.getText().insertTextContent(oCurs, oIndex, False)

End If

REM Even the newly inserted index is not updated until right HERE!

oIndex.update()

End Sub

The same code can be used to create a DocumentIndex, ContentIndex, 
UserDefinedIndex, IllustrationIndex, TableIndex, or ObjectIndex. 
Properties common to all index types are shown in Table 157.


This is the latest version on my web site as an ODT file. The document 
contains most of the macros in the book, so, you will be asked if you 
want to enable macros when  you load it. If you do not enable them, you 
cannot run the contained macros from buttons embedded in the text, which 
you probably do not care about so no worries if you do not enable that. 
Hope this helps.



On 2/8/19 8:42 AM, LORENZO Vincent wrote:
>
> Hello,
>
> I’m looking for a code example to create a table of contents and fill 
> it programmatically. I thought find something like an object called 
> XTableOfContents  in the unoil library, but nothing and Google didn’t 
> really help me.
>
> So, please if you some pointers, code examples, or advices they will 
> be welcome.
>
> Best Regards,
>
> -- 
>
> Vincent LORENZO
>
> 01-69-08-17-24
>
> CEA Saclay Nano-INNOV
>
> Institut CARNOT CEA LIST
>
> Point Courrier n° 174
>
> 91 191 Gif sur Yvette CEDEX
>
>
> _______________________________________________
> LibreOffice mailing list
> LibreOffice at lists.freedesktop.org
> https://lists.freedesktop.org/mailman/listinfo/libreoffice

-- 
Andrew Pitonyak
My Macro Document: http://www.pitonyak.org/AndrewMacro.odt
Info:  http://www.pitonyak.org/oo.php

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.freedesktop.org/archives/libreoffice/attachments/20190209/4ccbbdae/attachment.html>


More information about the LibreOffice mailing list