View Single Post
 
Old 11-25-2014, 11:19 PM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,467
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

That requires a rather different approach. Try the following macro. It converts your document to a table, which it then sorts by reference #. That way, all the references are gouped together. There do seem to be some anomalies/inconsistencies with your data, in that you sometimes have multiple names without references, followed by one or more references in a row, but not always the same number of references as names.

The output table sorts all reference series by the first reference in that series. It seems to produce consistent results.
Code:
Sub Demo()
Application.ScreenUpdating = False
With ActiveDocument
  With .Range
    With .Find
      .ClearFormatting
      .Replacement.ClearFormatting
      .Forward = True
      .Wrap = wdFindContinue
      .Format = False
      .MatchWildcards = True
      .Text = "\["
      .Replacement.Text = "^t"
      .Execute Replace:=wdReplaceAll
      .Text = "\]*[\;^13]"
      .Replacement.Text = "^p"
      .Execute Replace:=wdReplaceAll
      .Text = "\(*\)"
      .Replacement.Text = ""
      .Execute Replace:=wdReplaceAll
      .Text = "^s"
      .Replacement.Text = ""
      .Execute Replace:=wdReplaceAll
      .Text = ","
      .Wrap = wdFindStop
      .Execute
    End With
    Do While .Find.Found
      If .Hyperlinks.Count = 0 Then .Text = vbTab
      .Collapse wdCollapseEnd
      .Find.Execute
    Loop
  End With
  With .Range
    .ConvertToTable
    With .Tables(1)
      .Columns.Width = 36
      .Columns(1).Width = 144
      .Sort ExcludeHeader:=False, CaseSensitive:=False, LanguageID:=wdEnglishUS, _
        FieldNumber:="Column 2", SortFieldType:=wdSortFieldNumeric, SortOrder:=wdSortOrderAscending, _
        FieldNumber2:="Column 1", SortFieldType2:=wdSortFieldAlphanumeric, SortOrder2:=wdSortOrderAscending
    End With
  End With
End With
Application.ScreenUpdating = True
End Sub
Because your document is full of hyperlinks, doing any kind of processing that has to consider their display text (as would be required if you wanted to extract only those names associated with reference #34, for example) would be much more complicated than working with simple text.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote