Family name indexing macro for Open Office Writer

Purose

The purpose of this document is to provide a simple step by step way of automating the compilation of family-name indexes. The content incluudes the basic macro code and shows how to attach this to a speed-key so that indexing is a one key-stroke operation.

Background

When writing a family history document we often refer to, say, "John William Smith" and would like to have this indexed as "Smith, John William". The standard indexing in Writer will by default index "John William Smith" as "John William Smith". To change this manually to "Smith, John William" requires some effort each time we want to index an entry. Clearly there should be a better way.

The basic macro language that comes with Open Office facilitates what we want to achieve. The approach for MS Word is similar and set out here.

The starting point is to create a module in user macros. Do this by recording a macro. Then copy in the following code.

Sub index_fam()
Dim oSelction as Object
Dim oIndex as Object
Dim oDoc as Object
Dim oText as Object
Dim oVC as Object

oDoc = thisComponent
oVC = oDoc.CurrentController.getViewCursor
oText = oDoc.Text
oSelection = ThisComponent.CurrentController.Selection
oIndex =oDoc.createInstance("com.sun.star.text.DocumentIndexMark")
oString = oSelection(0)
Current = oVC.String

x = Trim(current)
if len(x) = 0 then exit sub
fore = ""
next_char = ""
i = 0
While next_char <> " " And i < Len(x)
    i = i + 1
    next_char = Left(Right(x, i), 1)
Wend
fam = Trim(Right(x, i))
If i < Len(x) Then
    fore = Trim(Left(x, Len(x) - i))
    fam = fam & ", " & fore
End If
   

    if HasUnoInterfaces(oString,"com.sun.star.text.XTextRange") Then
       oIndex.setMarkEntry(fam)
       oText.InsertTextContent(oString, oIndex, False)
    end If
End Sub

sub phrase_index
Dim oSelction as Object
Dim oIndex as Object
Dim oDoc as Object
Dim oText as Object
Dim oVC as Object

oDoc = thisComponent
oVC = oDoc.CurrentController.getViewCursor
oText = oDoc.Text
oSelection = ThisComponent.CurrentController.Selection
oIndex =oDoc.createInstance("com.sun.star.text.DocumentIndexMark")
oString = oSelection(0)
Current = oVC.String

x = Trim(current)
if len(x)=0 then exit sub
   

    if HasUnoInterfaces(oString,"com.sun.star.text.XTextRange") Then
       oIndex.setMarkEntry(x)
       oText.InsertTextContent(oString, oIndex, False)
    end If

end sub

The next step is to assign these macro to speed keys. {shift}{ctrl}M and {shift}{crtl}N are available unless you have already assinged something to them.

This is done by by using the Tool, Options menu and the Keyboard tab.
OpenOffice options keyboard settings
Select the macro first then assign it to the key by pressing the modify button.


© Nick Reddan 2010
My home
This page has been visited times since 6 October 2010