Macros/Writer/006

    From The Document Foundation Wiki
    < Macros‎ | Writer


    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

    In LibreOffice Basic:

    Option Explicit
    
    Sub UpdateIndexes()
        '''Update indexes, such as for the table of contents'''
    
        Dim i As Integer
    
        With ThisComponent ' Only process Writer documents
            If .supportsService("com.sun.star.text.GenericTextDocument") Then
                For i = 0 To .getDocumentIndexes().count - 1
                    .getDocumentIndexes().getByIndex(i).update()     
                Next i
            End If
        End With ' ThisComponent
    
    End Sub ' UpdateIndexes

    In Python:

    # -*- coding: utf-8 -*-
    from __future__ import unicode_literals
    
    import uno
    
    def UpdateIndexes():
        """Update indexes, such as for the table of contents"""
    
        thisComponent = XSCRIPTCONTEXT.getDocument()
        if thisComponent.supportsService("com.sun.star.text.GenericTextDocument"):
            count = thisComponent.getDocumentIndexes().getCount()
            for i in range(0,count):
                thisComponent.getDocumentIndexes().getByIndex(i).update()