Nick Reddan – Family name indexing macro for Word

Family name indexing macro for Word

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 visual basic macro code and shows how to attach this to a speed-key so that indexing is a one key-stroke operation.

Another variation of this approach with many enhancments.

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 word 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 visual basic macro language that comes with Word facilitates what we want to achieve. The approach for Open Office is similar and set out here.

Step1

First we have to record a macro into the normal.dotm (or normal.dot if using word 2003). We do this in the following manner depending on the version of word you are using.

Word 2007 or 2010

First turn on the "developer" ribbon. You do this by going to Word options and ticking the box next to "Show Developer tab in the Ribbon".

Select the Developer ribbon. Then click on record macro.
This opens the following dialogue box. Enter "mark_family" in the name of the macro then select "All document (Normal.dotm)" and "Keyboard" and press "OK".
This opens the following dialogue box. Click on the box under "Press a new shortcut key". Then press the "Shift", "Ctrl" and "M" keys at the same time. Then click on the "Assign" key.
type something then press the "stop recording buttton"  

Word 2003

Use the tools menu bar to initiate the recording of a macro. This opens the following dialogue box. Enter "mark_family" in the name of the macro then select "All document (Normal.dot)" and "Keyboard" and press "OK" This opens the following dialogue box. Click on the box under "Press a new shortcut key". Then press the "Shift", "Ctrl" and "M" keys at the same time. Then click on the "Assign" key. type something then press the "stop recording buttton"

Step2

Now we replace the recorded macro with the one we really want.

Word 2007

Open the Visual basic editor

Select the text between "Sub mark_family" and "End Sub".

 

Word 2003

Use the tools menu bar to open the visual basic editor. using the "Tools", "Macros" and "Visual basic" drop down menus.

Go to the module new macros in Normal.dot

Select the text between "Sub mark_family" and "End Sub".

Go to this window and, select and copy the following text.


'Macro written by Nick Reddan 2010 © used with permission
x = Trim(Selection.Text)
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
     ActiveDocument.Indexes.MarkEntry Range:=Selection.Range, Entry:= _
          fam, EntryAutoText:=fam, CrossReference:="", _
          CrossReferenceAutoText:="", BookmarkName:="", Bold:=False, Italic:=False


Go to the visual basic editor window and paste the text.

Close Word.

Test

Open Word and type "aaaa bbbb". Select "aaaa bbbb". Then press the Ctrl, Shift and "M" keys at the same time.

To index a phrase use the following code

'Macro written by Nick Reddan 2010 © used with permission
x = Trim(Selection.Text)
If Len(x) = 0 Then Exit Sub
     ActiveDocument.Indexes.MarkEntry Range:=Selection.Range, Entry:= _
          x, EntryAutoText:=x, CrossReference:="", _
          CrossReferenceAutoText:="", BookmarkName:="", Bold:=False, Italic:=False

This page has been visited times since 6 October 2010