Thread: [Solved] Reverse Paragraph List Macro
View Single Post
 
Old 08-16-2021, 04:25 PM
Guessed's Avatar
Guessed Guessed is offline Windows 10 Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,166
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

I think the message box at the start needs to be a bit more complex to deal with the likely selections: less than two paragraphs; includes a table
Code:
Sub ReverseOrderOfParagraphs()
  Dim tmpTable As Table, rg As Range
  
  If Selection.Range.Paragraphs.Count < 2 Then
    MsgBox "You must select more than one paragraph"
    Exit Sub
  ElseIf Selection.Range.Tables.Count > 0 Then
    MsgBox "This macro doesn't work with selections which are in tables"
    Exit Sub
  End If
  
  Set tmpTable = Selection.Range.ConvertToTable(Separator:=wdSeparateByParagraphs)
  With tmpTable
    .Columns.Add BeforeColumn:=.Columns(1)
    
    Set rg = .Cell(1, 1).Range
    rg.End = rg.End - 1
    rg.Fields.Add Range:=rg, Type:=wdFieldSequence, Text:="a"
    rg.Cells(1).Range.Copy
    
    .Columns(1).Select
    Selection.Paste
    Selection.Fields.Update
    
    .Sort ExcludeHeader:=False, FieldNumber:=1, SortOrder:=wdSortOrderDescending
    .Columns(1).Delete
    .ConvertToText
  End With
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote