Mark multiple occurrences of words for index

B

Bob Rankin

Hi all,

I created the following macro in Word 2002 to read the full name I had
selected, change the order to "last name, first name," then mark it
for an Index entry. This works fine. But I need to somehow modify the
code so that it will automatically index-mark every occurrence of the
name I select throughout the document. For example, if the name "Bill
Johnson" occurs 35 times throughout my document, I would like to be
able to select one occurrence of this name, and let the macro mark
this and ALL other occurrences of the name as index entries. Can
anyone help?

Thanks,

Bob




Sub ErrolMark()

Dim TextToMark, ChangedText, TempVar1, TempVar2 As String
Dim Counter1 As Integer

'Initialize counter variable.
Counter1 = 0

'Assign selected text to TextToMark variable.
TextToMark = Selection.Text

'Check for paragraph mark at end of selection and delete it.
If Right(TextToMark, 1) = Chr(13) Then
TextToMark = Left(TextToMark, Len(TextToMark) - 1)
End If

'Check for spaces at the end of the selection and delete them.
Do Until Right(TextToMark, 1) <> " "
TextToMark = Left(TextToMark, Len(TextToMark) - 1)
Loop

'Convert TextToMark variable to lastname, firstname and put into
ChangedText.
Do Until Left(TempVar1, 1) = " "
Counter1 = Counter1 + 1
TempVar1 = Right(TextToMark, Counter1)
Loop
TempVar1 = Right(TempVar1, Len(TempVar1) - 1)
TempVar2 = Left(TextToMark, Len(TextToMark) - Counter1)
ChangedText = TempVar1 & ", " & TempVar2

'Add index entry mark.
ActiveDocument.Indexes.MarkEntry Range:=Selection.Range, Entry:= _
ChangedText, EntryAutoText:="James A. Satterfield", _
CrossReference:="", CrossReferenceAutoText:="",
BookmarkName:="", Bold:= _
False, Italic:=False
End Sub
 
D

Doug Robbins - Word MVP

Hi Bob,

In another macro in the same project, use

Dim TexttobeMarked As String
TexttobeMark ed= InputBox("Enter the text that you wish mark.", "Indexer",
"")
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:=TexttobeMarked, MatchWildcards:=False,
Wrap:=wdFindContinue, Forward:=True) = True
Call ErrolMark
Loop
End With


--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
B

Bob Rankin

Thanks, Doug. It worked, except it never stops. I need it to stop
after it marks the last occurrence of the word/phrase, basically when
it hits the end of the document and needs to wrap around to the top
again. Could you take a look?

Thanks,

Bob
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top