Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Tables / August 2003

Tip: Looking for answers? Try searching our database.

sorting by word length

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Philip Morehead - 10 Aug 2003 12:58 GMT
I am working with a document in Word X for mac. Is there a way to sort a
list of words by word length?
Jay Freedman - 10 Aug 2003 17:08 GMT
> I am working with a document in Word X for mac. Is there a way to
> sort a list of words by word length?

Hi, Philip,

This macro should word equally well in Word for Mac or Windows:

Sub SortByWordLength()
   Dim nRow As Long
   Dim bPlainText As Boolean

   If Not Selection.Information(wdWithInTable) Then
       ' if the selected text is already in a table,
       ' skip the next part, otherwise we need to
       ' make it a table so we can add a temporary
       ' column for the lengths
       bPlainText = True

       ' ConvertToTable can't work on an
       ' insertion point, so select everything
       If Selection.Type = wdSelectionIP Then
           Selection.WholeStory
       End If

       ' make the table
       Selection.ConvertToTable numcolumns:=1
   End If

   With Selection.Tables(1)
       ' add the temporary column as new col. 1
       .Columns.Add beforecolumn:=.Columns(1)

       ' in each row, fill the first cell with the
       ' length of the (first or only) word in the
       ' second cell (formerly the first column)
       For nRow = 1 To .Rows.Count
           .Cell(nRow, 1).Range.Text = _
               Len(.Cell(nRow, 2).Range.Words(1))
       Next nRow

       ' sort on the first column (lengths)
       .Sort

       ' the lengths are no longer needed, so
       ' get rid of them
       .Columns(1).Delete

       ' if we started with plain text,
       ' convert back to text
       If bPlainText Then
           .ConvertToText
       End If
   End With
End Sub

If the starting selection is in a table that has any vertically merged
cells, the macro fails badly -- you get an error message saying that "Word
found no valid records to sort" and leaves the table with its extra column.
If the table contains horizontally merged cells, the macro fails but hasn't
yet made any changes in the document. These aren't problems if you never run
the macro in a document that contains a table with merged cells; otherwise
it needs some special error-trapping.

Signature

Regards,
Jay Freedman
Microsoft Word MVP     FAQ: http://www.mvps.org/word

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.