Solved

Microsoft Word - How to use Automark files

Posted on 2010-08-18
8
558 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
  • 4
  • 4
8 Comments
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 76

Expert Comment

by:GrahamSkan
Comment Utility
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 10

Author Comment

by:LennyGray
Comment Utility
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
Comment Utility
Graham is a miracle worker!
0
 
LVL 76

Expert Comment

by:GrahamSkan
Comment Utility
You need to add:

DocA.Fields.Update


to do what F9 does.
0
 
LVL 10

Author Comment

by:LennyGray
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Do you ever need to create a 20 page Word document for some testing purpose? Are you tired of copying & pasting old boring "lorem ipsum" text over and over again, increasing font size and line space in order to make the document 20+ pages long? Look…
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…
This video walks the viewer through the process of creating a watermark for their document, customizing it, and saving it for viewing/printing needs.
The viewer will learn how to make their project stand out over others by learning how to change colors and shapes, add spaces, change directions, and add bullets to their charts.

744 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now