Solved

Microsoft Word - How to use Automark files

Posted on 2010-08-18
8
576 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 500 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
On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

 
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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Nice table. Huge mess. Maybe this was something you created way back before you figured out tabs or a document you received from someone else. Either way, using the spacebar to separate the columns resulted in a mess. Trying to convert text to t…
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
This video walks the viewer through the process of creating an MLA formatted document, as well as a bibliography with citations.
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …

617 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