Microsoft Office Forums Find longest paragraph in Word document

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 09-10-2019, 02:54 PM
keimba keimba is offline Find longest paragraph in Word document Windows 10 Find longest paragraph in Word document Office 2019
Novice
Find longest paragraph in Word document
 
Join Date: Sep 2019
Posts: 2
keimba is on a distinguished road
Default Find longest paragraph in Word document

I'd like to find the longest paragraph in the currently active Word document. Perhaps someone here can give me suggestions for VBA code.

I want to be able to activate a macro that finds and then selects the longest paragraph in the document. Even better, activating a second time causes the macro change the selection to the second longest paragraph and so on.

My current thoughts are to create a 2 x n array that is populated with the paragraph numbers and the corresponding word counts, sort the array by word counts, and then set the active selection to corresponding paragraph number. But I don't know how to implement this and perhaps there's a better way to get to the same solution.



Thanks!
Reply With Quote
  #2  
Old 09-10-2019, 04:02 PM
gmaxey gmaxey is offline Find longest paragraph in Word document Windows 10 Find longest paragraph in Word document Office 2016
Word MVP 2003-2009
 
Join Date: May 2010
Location: Marble, NC
Posts: 962
gmaxey will become famous soon enoughgmaxey will become famous soon enough
Default

you could do that with a System.Collections.SortedList and a Scripting.Dictionary:


Code:
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Static lngRun As Long
Dim lngIndex As Long
Dim oSL As Object, oDic As Object
Dim oPar As Paragraph

ReEntry:
  Set oSL = CreateObject("System.Collections.SortedList")
  Set oDic = CreateObject("Scripting.Dictionary")
  For Each oPar In ActiveDocument.Paragraphs
    If Not oSL.Contains(Len(oPar.Range.Text)) Then
      oSL.Add Len(oPar.Range.Text), oPar.Range
    Else
      oDic.Add oPar.Range, Len(oPar.Range.Text)
    End If
  Next
  On Error GoTo Reset
  For lngIndex = 1 To lngRun
    oSL.RemoveAt oSL.Count - 1
  Next lngIndex
  lngRun = lngRun + 1
  oSL.GetByIndex(oSL.Count - 1).Select
  On Error GoTo 0
  For lngIndex = 0 To oDic.Count - 1
    Debug.Print oDic.Keys()(lngIndex) & " " & oSL.Getkey(oSL.Count - 1)
    If oDic.Items()(lngIndex) = oSL.Getkey(oSL.Count - 1) Then
      MsgBox "Other paragraphs were found with the same length."
      Exit For
    End If
  Next lngIndex
lbl_Exit:
  Set oSL = Nothing: Set oDic = Nothing
  Exit Sub
Reset:
  lngRun = 0
  If MsgBox("You have reached to the shortest paragraph. Do yo want to exit", vbYesNo, "LOOP") = vbYes Then
    Resume lbl_Exit
  Else
    Resume ReEntry
  End If
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #3  
Old 09-11-2019, 03:26 PM
keimba keimba is offline Find longest paragraph in Word document Windows 10 Find longest paragraph in Word document Office 2019
Novice
Find longest paragraph in Word document
 
Join Date: Sep 2019
Posts: 2
keimba is on a distinguished road
Default Thank you.

That works very well. I didn't know about System.Collections.SortedList and Scripting.Dictionary.
Reply With Quote
Reply

Tags
paragraph, sort, vba

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Find several words in document, copy paragraph and create new document coolio2341 Word VBA 6 01-31-2019 01:17 PM
Find longest paragraph in Word document Find word, Insert Paragraph and bookmark gattaca714 Word VBA 2 03-24-2017 09:23 PM
Find Word then Highlight Whole Sentence and Paragraph Around it ChrisOK Word VBA 4 09-08-2016 10:16 PM
Arrange shortest to longest sentence in Word omar Word 8 11-20-2015 04:38 PM
Find last word in paragraph and delete it Dave T Word VBA 3 05-21-2015 12:40 AM


All times are GMT -7. The time now is 04:48 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2019, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2019 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft