Macros/Writer/006

From The Document Foundation Wiki
< Macros‎ | Writer
Jump to: navigation, search

Template:TopMenu/en Template:Menu/en Template:Menu.Macros/en

Other languages:

Description

There is often a problem that indexes, such as the table of contents (which is a type of index), aren't updated after changes have occured. Documents can be sent with incorrect links.

This macro updates automatically the links in an index. It can be useful to link the macro to the command Save document (Extras ▸ Customize ▸ Events ▸ Save document), so that the links will be updated when the document is saved.

In the example of Table of Contents, imagine that you have created a document with various headers (which are assigned by right-click and Paragraph ▸ Heading 1, etc). And then now you insert a TOC by Insert ▸ Table of Contents and Index ▸ Table of Contents, Index or Bibliography.... Then if you add more headers, you can update the TOC by simply running this macro.

Code

Option Explicit

Sub UpdateIndexes()
    '''Update indexes, such as for the table of contents'''

If ThisComponent.supportsService("com.sun.star.text.GenericTextDocument") Then
    For i = 0 To ThisComponent.getDocumentIndexes().count - 1
        ThisComponent.getDocumentIndexes().getByIndex(i).update()     
    Next i
End If

End Sub ' UpdateIndexes