Question : Microsoft Word - How to use Automark files

I have code that creates an Automark file. How do I use it?

Below is the code that I installed in the VB editor of Word.

When I enter a new sentence that contains a word that was previously indexed and run this code, the index at the end of the document does not reflect the word in the new sentence with the previously-indexed word.

Please help this MsWord casual user.

Thanks!

Lenny
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
Option Explicit

Sub CreateAutoMarkFile()
Dim fld As Field
Dim strText As String
Dim rw As Row
Dim tbl As Table
Dim bFound As Boolean
Dim doc As Word.Document
Dim DocA As Document

Set DocA = ActiveDocument
Set doc = Documents.Add
Set tbl = doc.Tables.Add(doc.Range, 1, 2)
For Each fld In DocA.Fields
    If fld.Type = wdFieldIndexEntry Then
        strText = GetIndexText(fld)
        bFound = False
        For Each rw In tbl.Rows
            If GetCellText(rw.Cells(1)) = strText Then
                bFound = True
                Exit For
            End If
        Next rw
        If Not bFound Then
            If Len(tbl.Rows.Last.Range) = 6 Then
                Set rw = tbl.Rows.Last
            Else
                Set rw = tbl.Rows.Add
            End If
            rw.Cells(1).Range.Text = strText
            rw.Cells(2).Range.Text = strText
        End If
    End If
Next fld
End Sub

Answer : Microsoft Word - How to use Automark files

Actually Lenny, teylyn had already explained how to do the job manually. Perhaps her use if the term 'concordance' file, was a bit confusing, but Microsoft sometimes calls it that as well.

All I did was to provide some automation for the fiddly part.

I have now added some code to automate the use of the file as well and its creation.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
ption Explicit


Sub CreateAutoMarkFile()
Dim fld As Field
Dim strText As String
Dim rw As Row
Dim tbl As Table
Dim bFound As Boolean
Dim doc As Word.Document
Dim DocA As Document

Set DocA = ActiveDocument
Set doc = Documents.Add
Set tbl = doc.Tables.Add(doc.Range, 1, 2)
For Each fld In DocA.Fields
    If fld.Type = wdFieldIndexEntry Then
        strText = GetIndexText(fld)
        bFound = False
        For Each rw In tbl.Rows
            If GetCellText(rw.Cells(1)) = strText Then
                bFound = True
                Exit For
            End If
        Next rw
        If Not bFound Then
            If Len(tbl.Rows.Last.Range) = 6 Then
                Set rw = tbl.Rows.Last
            Else
                Set rw = tbl.Rows.Add
            End If
            rw.Cells(1).Range.Text = strText
            rw.Cells(2).Range.Text = strText
        End If
    End If
Next fld

doc.SaveAs "C:\MyFolder\MyAutoMark.doc"
doc.Close wdDoNotSaveChanges
DocA.Indexes.AutoMarkEntries "C:\MyFolder\MyAutoMark.doc"

End Sub

Function GetCellText(cl As Word.Cell) As String
    Dim rng As Range
    Set rng = cl.Range
    rng.MoveEnd wdCharacter, -1
    GetCellText = rng.Text
End Function

Function GetIndexText(fld As Word.Field) As String
    Dim p As Integer
    Dim q As Integer
    Dim strText As String
    
    strText = fld.Code.Text
    p = InStr(strText, """")
    q = InStrRev(strText, """")
    GetIndexText = Mid$(strText, p + 1, q - p - 1)
End Function
Random Solutions  
 
programming4us programming4us