View Single Post
 
Old 08-19-2020, 01:06 PM
grumblid grumblid is offline Windows 7 64bit Office 2003
Novice
 
Join Date: Jul 2016
Posts: 25
grumblid is on a distinguished road
Default

Quote:
Originally Posted by Guessed View Post
Based on that sample doc, this code appears to work
Code:
Sub SortParasBySize()
  Dim aRng As Range, aTable As Table, aRow As Row
  ActiveWindow.View = wdNormalView
  With ActiveDocument.Range.Find  'Do Find and Replace for separators
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "^p"
    .Replacement.Text = "zx"
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    .Execute Replace:=wdReplaceAll
    .Text = "zxzx"
    .Replacement.Text = "^p"
    .Execute Replace:=wdReplaceAll
    ActiveDocument.Range.ConvertToTable Separator:=wdSeparateByParagraphs, NumColumns:=1
    .Text = "zx"
    .Replacement.Text = "^p"
    .Execute Replace:=wdReplaceAll
  End With
  Set aTable = ActiveDocument.Tables(1)
  aTable.Columns.Add BeforeColumn:=aTable.Columns(1)
  For Each aRow In aTable.Rows
    aRow.Cells(1).Range.Text = Len(aRow.Cells(2).Range.Text)
  Next aRow
  aTable.Rows.Add BeforeRow:=aTable.Rows(1)
  aTable.SortDescending
  aTable.Columns(1).Delete
  aTable.Columns.Add    'insert empty column to reinstate extra paras between sections
  aTable.Rows(1).Delete
  aTable.ConvertToText Separator:=wdSeparateByParagraphs
End Sub
It works! owo

Wow, you're awesome dude. Thank you so much for taking the time to make such a useful tool. And sorry it took me so long to provide you a document so you could test it properly :]

The only unexpected thing it does is it changes the view from the Print Layout View (which I usually use) to the Normal View, seen at the very bottom left of the screen. Which isn't a big deal at all; I can just click Print Layout View again and it's back~
Reply With Quote