Hi, how do I sort on last word of selection in Word.
I want to select a group of single lines (as below) about 1,000 and sort by
last word only? I know I can I never remember how! Thanks in advance for any
help.

objCC.DropdownListEntries.Add "Dr Edward Rowel"

objCC.DropdownListEntries.Add "Dr Michael Adams"

Re: F&R or Macro sort on last word in a line for selection by Helmut

Helmut
Tue May 13 08:35:56 PDT 2008

Hi Jen,

assuming that your lines are paragraphs,
structured like that:
objCC.DropdownListEntries.Add "Dr Edward Rowel"¶
objCC.DropdownListEntries.Add "Dr Michael Adams"¶

Then I'd put the second last word in a paragraph
at the start of that paragraph.
Note that the paragraph mark is the last word
in a paragraph and that the closing quotation mark
is the second last word.
So the name is the third last word:
oprg.range.Words.count - 2

For Each oPrg In Selection.Paragraphs
sTmp = oPrg.Range.Words(oPrg.Range.Words.Count - 2)
oPrg.Range.InsertBefore sTmp & " "
Next

Then I'd sort the paragraphs:
Selection.Sort

Then I'd remove the first word again:
For Each oPrg In Selection.Paragraphs
oPrg.Range.Words(1).Delete
Next


All together:

Sub SortMe()
Dim sTmp As String
Dim oPrg As Paragraph

For Each oPrg In Selection.Paragraphs
sTmp = oPrg.Range.Words(oPrg.Range.Words.Count - 2)
oPrg.Range.InsertBefore sTmp & " "
Next
Selection.Sort
For Each oPrg In Selection.Paragraphs
oPrg.Range.Words(1).Delete
Next
End Sub


Which is far from being perfect,
just quick and dirty,
but setting up a quicksort,
working on the second last word in a paragraph,
was too much for me here and now.

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP

Re: F&R or Macro sort on last word in a line for selection by Jen

Jen
Tue May 13 16:36:34 PDT 2008

Thank you Helmet - forgot about the quotation mark - it could have been
dropped but quick and dirty was all that is required.


"Helmut Weber" <red.sys@t-online.de> wrote in message
news:ncaj24178022ckpkh5qm6t2ja6kt9rqmgf@4ax.com...
> Hi Jen,
>
> assuming that your lines are paragraphs,
> structured like that:
> objCC.DropdownListEntries.Add "Dr Edward Rowel"¶
> objCC.DropdownListEntries.Add "Dr Michael Adams"¶
>
> Then I'd put the second last word in a paragraph
> at the start of that paragraph.
> Note that the paragraph mark is the last word
> in a paragraph and that the closing quotation mark
> is the second last word.
> So the name is the third last word:
> oprg.range.Words.count - 2
>
> For Each oPrg In Selection.Paragraphs
> sTmp = oPrg.Range.Words(oPrg.Range.Words.Count - 2)
> oPrg.Range.InsertBefore sTmp & " "
> Next
>
> Then I'd sort the paragraphs:
> Selection.Sort
>
> Then I'd remove the first word again:
> For Each oPrg In Selection.Paragraphs
> oPrg.Range.Words(1).Delete
> Next
>
>
> All together:
>
> Sub SortMe()
> Dim sTmp As String
> Dim oPrg As Paragraph
>
> For Each oPrg In Selection.Paragraphs
> sTmp = oPrg.Range.Words(oPrg.Range.Words.Count - 2)
> oPrg.Range.InsertBefore sTmp & " "
> Next
> Selection.Sort
> For Each oPrg In Selection.Paragraphs
> oPrg.Range.Words(1).Delete
> Next
> End Sub
>
>
> Which is far from being perfect,
> just quick and dirty,
> but setting up a quicksort,
> working on the second last word in a paragraph,
> was too much for me here and now.
>
> --
>
> Greetings from Bavaria, Germany
>
> Helmut Weber, MVP WordVBA
>
> Vista Small Business, Office XP