You are here: Scripting Techniques > Common Script Operations > Creating secondary index keywords in a script

Creating secondary index keywords in a script

To create secondary index keywords, use the Add method of the Keywords collection and specify the parent keyword as the second argument. The following example creates a top-level keyword and then creates a secondary keyword as its child:

Dim key

Set key = Keywords.Add("mammals")

Keywords.Add "Homo sapiens", key

Note the use of the variable key as a placeholder for the top-level Keyword object. Keywords created in this manner are not associated with any topics. This is analogous to creating secondary keywords in the Index toolbar.

However, if you use the Keywords collection of the active topic, and not of the project, then both top-level and secondary keywords are created and associated with the active topic:

Dim key

Set key = ActiveTopic.Keywords.Add("mammals")

ActiveTopic.Keywords.Add "Homo sapiens", key

This is analogous to using the Index command of the Topic menu twice, once for each keyword. Note that you can simplify the script and make it more efficient by assigning the Keywords collection of the ActiveTopic object to a variable:

Dim key, keys

Set keys = ActiveTopic.Keywords

Set key = keys.add("mammals")

keys.add "Homo sapiens", key