Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 586
  • Last Modified:

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
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

Open in new window

0
LennyGray
Asked:
LennyGray
  • 4
  • 4
1 Solution
 
GrahamSkanCommented:
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.
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

Open in new window

0
 
LennyGrayAuthor Commented:
Hi Graham -

Thanks for the automation part. I was looking for a turnkey solution for this.

Attached is the document that is being worked on. I added "Directories To Process" on page 26 but the addition of that term is not being used inthe reindexing, although it was originally indexed and appears inthe index for page 2 only.

Can you look at this and make it so that all I have to do is click on the function to run it?

Thanks!

Lenny
Extractor-Reference-Manual-with-.doc
0
 
GrahamSkanCommented:
Hmm,

To confirm your experience, it doesn't work for me, either, with your document.  I have no idea why not, so I'll be a while analysing it.

It won't affect the work that I invest here, but I'm curious to know if it is in support of one or more of our charities.
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
GrahamSkanCommented:
Well, it must be the date!!!!

I tried expanding from my test document, getting closer and closer to your exact document until it failed, but it never did.

It now works with your document every time.

Does it still fail for you?
0
 
LennyGrayAuthor Commented:
Hi Graham -

I assume that I simply open the code and run CreateAutoMarkFile() and this will recreate the index.

I did that and nothing changed.

But I just discovered that by highlighting the index and then hitting F9, everything refreshes and your code works!

You really are a genius with Word!

Is there a way to automate the F9 key stoke after the code runs?

Thanks, again, my friend!
0
 
LennyGrayAuthor Commented:
Graham is a miracle worker!
0
 
GrahamSkanCommented:
You need to add:

DocA.Fields.Update


to do what F9 does.
0
 
LennyGrayAuthor Commented:
Thanks!

Stay healthy and keep happiness close-at-hand.

“You cannot control what happens to you, but you can control your attitude toward what happens to you, and in that, you will be mastering change rather than allowing it to master you.”

Lenny
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now