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