blob: aa0aba6fad19f698fbe9aa784ff90950a9680226 [file] [log] [blame]
<!--
$RCSfile: Writer.InsertModifyContentTable.snip,v $
last change: $Revision: 1.1 $ $Author: tomsontom $ $Date: 2004/06/24 13:30:40
$
(c)2003 by the copyright holders listed with the author-tags.
If no explicit copyright holder is mentioned with a certain author,
the author him-/herself is the copyright holder. All rights reserved.
Public Documentation License Notice:
The contents of this Documentation are subject to the
Public Documentation License Version 1.0 (the "License");
you may only use this Documentation if you comply with
the terms of this License. A copy of the License is
available at http://www.openoffice.org/licenses/PDL.html
The Original Documentation can be found in the CVS archives
of openoffice.org at the place specified by RCSfile: in this header.
The Initial Writer(s) of the Original Documentation are listed
with the author-tags below.
The Contributor(s) are listed with the author-tags below
without the marker for being an initial author.
All Rights Reserved.
-->
<snippet language="OOBasic" application="Writer">
<keywords>
<keyword>index</keyword>
<keyword>table of contents</keyword>
<keyword>ContentIndex</keyword>
<keyword>getDocumentIndexes</keyword>
<keyword>CreateFromOutline</keyword>
</keywords>
<authors>
<author id="pitonyak" initial="true" email="andrew@pitonyak.org">Andrew Pitonyak</author>
</authors>
<question heading="Create and insert or modify an existing table of
contents or index">
How can I insert a table of contents into my document?
</question>
<answer>
<p>
Create a com.sun.star.text.ContentIndex from the document and then
insert the text content.
</p>
<p>Be certain to set the appropriate properties. I have some discussion
at: <a href="http://www.oooforum.org/forum/viewtopic.php?t=10251">http://www.oooforum.org/forum/viewtopic.php?t=10251</a></p>
<listing>
Sub InsertATOC
REM Author: Andrew Pitonyak
Dim oCurs &apos;Used to insert the text content.
Dim oIndexes &apos;All of the existing indexes
Dim oIndex &apos;The TOC if it exists and a new one if not
Dim i As Integer &apos;Find an existing TOC
Dim bIndexFound As Boolean &apos;Flag to track if the TOC was found
REM First, find an existing TOC if it exists. If so,
REM then this will simply be updated.
oIndexes = ThisComponent.getDocumentIndexes()
bIndexFound = False
For i = 0 To oIndexes.getCount() - 1
oIndex = oIndexes.getByIndex(i)
If oIndex.supportsService(&quot;com.sun.star.text.ContentIndex&quot;) Then
bIndexFound = True
Exit For
End If
Next
If Not bIndexFound Then
Print &quot;I did not find an existing content index&quot;
REM Perhaps you should create and insert a new one!
REM Notice that this MUST be created by the document that will
contain
REM the index.
oIndex = ThisComponent.createInstance(&quot;com.sun.star.text.ContentIndex&quot;)
REM On my system, these are the default values
REM How do you want to create the index?
REM CreateFromChapter = False
REM CreateFromLevelParagraphStyles = False
REM CreateFromMarks = True
REM CreateFromOutline = False
oIndex.CreateFromOutline = True
REM You can set all sorts of other things such as the
REM Title or Level
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
</listing>
</answer>
<changelog>
<change author-id="pitonyak" date="2004-06-29">Initial version</change>
</changelog>
</snippet>