Time enough for a quick bandaid. I added another utility that will take
names in lists formatted like:
George Washington IX
Thomas Jefferson VII
James Madison IV
and covert them to:
George Washington, IX
Thomas Jefferson, VII
James Madison, IV
For sorting as:
Jefferson, Thomas, VII
Madison, James, IV
Washington, George, IX
Then the second macro will swap arrangement and remove the improper comma
separation:
Thomas Jefferson VII
James Madison IV
George Washington IX
Option Explicit
Dim oPar As Paragraph
Dim oRng As Word.Range
Dim oRngSelected As Range
Dim bEndofDoc As Boolean
Sub ArrangeAndSortNames()
Set oRngSelected = Selection.Range
If oRngSelected.Paragraphs.Count < 2 Then
MsgBox "There is no valid selection to sort"
Exit Sub
End If
bEndofDoc = False
If oRngSelected.End = ActiveDocument.Range.End Then
bEndofDoc = True
oRngSelected.InsertAfter vbCr
oRngSelected.MoveEnd wdParagraph, -1
End If
SetRemoveFlags oRngSelected, True
NumericalSuffixes oRngSelected, True
For Each oPar In oRngSelected.Paragraphs
Set oRng = oPar.Range
If InStr(oPar.Range.Text, ",") > 0 Then
oRng.End = oRng.Start + InStr(oPar.Range.Text, ",") - 1
oRng.InsertBefore Trim(oRng.Words(oRng.Words.Count)) & ", "
oRng.Words(oRng.Words.Count).Delete
Else
oRng.End = oRng.End - 1
oRng.InsertBefore Trim(oRng.Words(oRng.Words.Count)) & ", "
oRng.Words(oRng.Words.Count).Delete
End If
Next
oRngSelected.Sort ExcludeHeader:=False, FieldNumber:="Paragraphs", _
SortFieldType:=wdSortFieldAlphanumeric, SortOrder:=wdSortOrderAscending, _
FieldNumber2:="", SortFieldType2:=wdSortFieldAlphanumeric, SortOrder2:= _
wdSortOrderAscending, FieldNumber3:="", SortFieldType3:= _
wdSortFieldAlphanumeric, SortOrder3:=wdSortOrderAscending, Separator:= _
wdSortSeparateByTabs, SortColumn:=False, CaseSensitive:=False, LanguageID
_
:=wdEnglishUS
SetRemoveFlags oRngSelected, False
Selection.Collapse wdCollapseStart
If bEndofDoc Then ActiveDocument.Range.Paragraphs.Last.Range.Delete
End Sub
Sub ArrangeFirstThenLast()
Dim pStr As String
Set oRngSelected = Selection.Range
bEndofDoc = False
If oRngSelected.End = ActiveDocument.Range.End Then
bEndofDoc = True
oRngSelected.InsertAfter vbCr
oRngSelected.MoveEnd wdParagraph, -1
End If
SetRemoveFlags oRngSelected, True
For Each oPar In oRngSelected.Paragraphs
Set oRng = oPar.Range
With oRng
pStr = " " & .Words.First.Text
.Collapse wdCollapseStart
.End = .Words.First.End + 2
.Delete
End With
Set oRng = oPar.Range
If InStr(oPar.Range.Text, ",") > 0 Then
oRng.End = oRng.Start + InStr(oPar.Range.Text, ",") - 1
oRng.InsertAfter pStr
Else
oRng.End = oRng.End - 1
oRng.InsertAfter pStr
End If
Next
SetRemoveFlags oRngSelected, False
NumericalSuffixes oRngSelected, False
Selection.Collapse wdCollapseStart
If bEndofDoc Then ActiveDocument.Range.Paragraphs.Last.Range.Delete
End Sub
Sub SetRemoveFlags(ByRef oRngSearch As Word.Range, bSet As Boolean)
Dim vOrigText As Variant
Dim vFlagText As Variant
Dim i As Long
vOrigText = Array("-", Asc(30))
vFlagText = Array("XxXx", "YyYy")
With oRngSearch.Find
For i = 0 To UBound(vOrigText)
Select Case bSet
Case True
.Text = vOrigText(i)
.Replacement.Text = vFlagText(i)
Case Else
.Text = vFlagText(i)
.Replacement.Text = vOrigText(i)
End Select
.Execute Replace:=wdReplaceAll
Next i
End With
End Sub
Sub NumericalSuffixes(ByRef oRngSearch As Word.Range, bSet As Boolean)
Dim vOrigText As Variant
Dim i As Long
vOrigText = Array("III", "IV", "V", "VI", "VII", "VII", "IX", "X", "XI",
"XII", "XII", "XIV", _
"XV")
With oRngSearch.Find
.MatchWholeWord = True
.MatchWildcards = True
For i = 0 To UBound(vOrigText)
Select Case bSet
Case True
.Text = "([!,]) (" & vOrigText(i) & ")"
.Replacement.Text = "\1, \2"
Case Else
.Text = ", " & vOrigText(i)
.Replacement.Text = " " & vOrigText(i)
End Select
.Execute Replace:=wdReplaceAll
Next i
End With
End Sub