none
How to count unknown number of paragraphs using Find "<text>" RRS feed

  • Question

  • I have an unknown amount of paragraphs in a document (the document is based off of an export from another software). The paragraphs are all on the first page, beginning at the top of the document. I need to count from Line 1 to Line unknown. I was trying to use a find and replace type approach with a Do Until loop to count from the top of the document down to the paragraph where the marker I put in for the find command is. I am unsure if this is possible and the syntax to do so. Any advice would be greatly appreciated!

    Thursday, January 31, 2013 4:54 PM

All replies

  • This will count the paragraphs, though it will double count if you use two returns to space paragraphs.   

        Selection.Find.Text = "<t"
        Selection.Find.Execute
        Selection.HomeKey Unit:=wdStory, Extend:=wdExtend
        MsgBox Selection.Paragraphs.Count

    Thursday, January 31, 2013 5:21 PM
  • Thank you for the snippet of code, it works great. However my concern now is assigning that number counted to a variable so that I can make a range. Here is what I want.

    1. Count the paragraphs at the top of the document --Done with the code you gave

    2. Select the Counted paragraphs using a range, copy, paste in a different location in the document and then delete the orgininal text at the top of the document.

    I have the code for 2.  I just need to take your code and be able to set the line: "MsgBox Selection.Paragraphs.Count" as an integer value and plug that into my code for the Paragraphs().Range.End part

    I tried:

    Dim nPara As Integer

     nPara = Selection.Paragraphs.Count

    but I get the error: "Object Required"

    When I try nPara = ActiveDocument.Selection.Paragraphs.Count I get the error: "Object doesn't support this property or method"

    What can I do about this?

    Thursday, January 31, 2013 7:05 PM
  • You need to do it this way:

    Dim nPara As Integer
    Selection.Find.Text = "<Text>"
    Selection.Find.Execute
    Selection.HomeKey Unit:=wdStory, Extend:=wdExtend
    nPara = Selection.Paragraphs.Count
    MsgBox nPara

    But you don't need to count paragraphs to do what you want - if the different location is just before "<insert here>"

    Selection.Find.Text = "<Text>"
    Selection.Find.Execute
    Selection.HomeKey Unit:=wdStory, Extend:=wdExtend
    Selection.Cut
    Selection.Find.Text = "<insert here>"
    Selection.Find.Execute
    Selection.MoveLeft Unit:=wdCharacter, Count:=1
    Selection.PasteAndFormat (wdPasteDefault)

    Thursday, January 31, 2013 7:35 PM
  • Yes you are right, the second way is more efficient, my issue is positioning the cursor to the top of the document to get the correct selection of paragraphs (due to the import of the other software the cursor is by default at the end of the document).

    I thought of adding another text marker, finding the marker and then counting the selection from there but that is leading to errors. Is there a more efficient way to jump to the top of the document?

    Thursday, January 31, 2013 8:31 PM
  • Try

    Selection.HomeKey Unit:=wdStory, Extend:=wdMove

    Thursday, January 31, 2013 9:04 PM
  • Excellent, very helpful, thank you.

    Friday, February 1, 2013 2:05 PM