I have a large document that I created, and some of the paragraphs are quite large. Now I want to programmatically loop through each paragraph, and if a paragraph has too many words, then I want to go to that paragraph so that I can manually break it up or simplify it.
Looping through the paragraphs is not a problem; getting the number of words for each paragraph is not a problem.
But going to that paragraph is insanely difficult.
Much of the examples around the internet suggest this code:
Sub GotoParagraph10()
Dim paragraphNumber as Integer
paragraphNumber = 10
Selection.GoTo What:=wdGoToParagraph, Which:=wdGoToAbsolute, Count:=paragraphNumber
End Sub
This code tries to go to paragraph 10. The problem is that wdGoToParagraph is not defined in the WdGotoItem list - despite wide reference to this item. This does show up in Intellisense, and when used at runtime, the constant resolves to -1 and the code thinks I'm trying to go to a bookmark, not a paragraph number.
Ok, so I got it - I can't use this constant. How, then, do I programmatically go to a specific paragraph?
Try this out
Public Sub SelectParagraphByIndex(Index As Long)
Dim Paragraph As Word.Paragraph
Dim i As Long
For Each Paragraph In ActiveDocument.Paragraphs
i = i + 1
If i = Index Then
Paragraph.Range.Select
Exit For
End If
Next
End Sub
'Call from here
Public Sub SelectTester()
SelectParagraphByIndex 10
End Sub