Thread: [Solved] Schrödinger's paragraph
View Single Post
 
Old 12-06-2024, 01:38 PM
gmaxey gmaxey is offline Windows 10 Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,617
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default

I think you are adding more bells and whistles than your really need. There is really no need to evaluate every paragraph. Perhaps something like:

Code:
Sub DeleteEmptyParagraphs()
Dim oRng As Range
Dim lngST As Long, lngCount As Long, lngIndex As Long
Dim oPar As Paragraph
  lngST = Timer
  Set oRng = ActiveDocument.Range
  With oRng.Find
    .Text = Chr(13) & "{2,}"
    .MatchWildcards = True
    While .Execute
      For lngIndex = oRng.Paragraphs.Count To 1 Step -1
        Set oPar = oRng.Paragraphs(lngIndex)
        If Len(oPar.Range.Text) = 1 Then
          oPar.Range.Select
          Select Case AscW(oPar.Range.Characters.Last.Next)
            Case 12 'Do nothing
            Case Else
              oPar.Range.Delete
              lngCount = lngCount + 1
          End Select
        End If
      Next lngIndex
      oRng.Collapse wdCollapseEnd
    Wend
  End With
  MsgBox "Paragraphs deleted: " & lngCount & "  |  Execution time: " & _
              Round(Timer - lngST, 2) & " s  |  " & Format((Timer - lngST) / 60, "0.00") & " min"
lbl_Exit:
  Exit Sub
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote