Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Microsoft Word - How to use Automark files

Posted on 2010-08-18
8
Medium Priority
?
580 Views
Last Modified: 2012-05-10
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
Comment
Question by:LennyGray
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 2000 total points
ID: 33464240
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
 
LVL 10

Author Comment

by:LennyGray
ID: 33465755
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
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 33469037
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
[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

 
LVL 76

Expert Comment

by:GrahamSkan
ID: 33477526
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
 
LVL 10

Author Comment

by:LennyGray
ID: 33477759
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
 
LVL 10

Author Closing Comment

by:LennyGray
ID: 33477766
Graham is a miracle worker!
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 33478127
You need to add:

DocA.Fields.Update


to do what F9 does.
0
 
LVL 10

Author Comment

by:LennyGray
ID: 33478534
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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Shortcuts in Word Just the other day I had a training for Microsoft and they wanted me to show how well the new Windows and Office behaved on a touch device, which by the way is great, but it was only then that I realized that using keyboard shortc…
A few years ago I was very much a beginner at VBA, and that very much remains the case today.  I'll do my best to explain things as I go in the hope that other beginners can follow.  If you just want to check out a tool that creates a Select Case fu…
This video shows the viewer how to set up and create Footnotes in their document. Click on the References tab: Select "Insert Footnote": Type in desired text:
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question