locked
Number of paragraphs in word document!! RRS feed

  • Question

  • In a word document, we get the number of paragraphs by writing following line of code in QTP(automation tool for functional testing):

    wrdDoc.Paragraphs.Count

    wrdDoc.paragraphs.count is a call to the office document model

    But it is not equal to the number of paragraphs that we get by pressing word count button in review tab in word document

    Can anyone please let me know, why these number of paragraphs for the same document are different and how can they be same?

    This behavior can be reproduced inside Word Macro also

    This is also happening with number of characters in a word document (compared both with spaces and without spaces).

    wrdDoc.ActiveDocument.characters.count

    This seems to be an issue with MS Word. Can anyone please check?

    I am using MS Word 2007 and the word document is saved in Microsoft 97-2003 extension document

    Friday, May 11, 2012 2:19 PM

Answers

  • The following function counts the number of times the specified character occurs in (the body of) the active document:

    Function CountChar(c As String)
        Dim s As String
        Dim p As Long
        Dim q As Long
        s = ActiveDocument.Range.Text
        p = Len(s)
        q = Len(Replace(s, c, ""))
        CountChar = p - q
    End Function

    Here is an example of its use:

    Sub Demo()
        Dim n As Long
        ' Paragraph marks
        n = CountChar(vbCr)
        MsgBox "The total number of end-of-paragraph marks is " & n
        ' Cell markers
        n = CountChar(Chr(7))
        MsgBox "The total number of end-of-cell markers is " & n
    End Sub


    Regards, Hans Vogelaar

    • Marked as answer by Dummy yoyo Friday, May 25, 2012 7:44 AM
    Wednesday, May 16, 2012 10:25 AM

All replies

  • The Word Count dialog doesn't include empty paragraphs in the count. You can get this count programmatically as follows:

    Dim lngParagraphCount As Long
    lngParagraphCount = wrdDoc.BuiltInDocumentProperties(wdPropertyParas)

    To get the number of characters, use wdPropertyCharacters. or wdPropertyCharsWSpaces.

    Regards, Hans Vogelaar

    Friday, May 11, 2012 2:29 PM
  • Thanks Hans for the reply. However, can you please let me know if the above code is in VBScript as it is giving error when I run this code in QTP?

    Also, this issue is also happening with number of characters. How can that be possible?


    Vijay


    Friday, May 11, 2012 2:54 PM
  • In VBScript, remove the Dim lngParagraphCount As Long line, and replace the wdProperty... constants with their values:

    wdPropertyParas = 24

    wdPropertyCharacters = 16

    wdPropertyCharsWSpaces = 30


    Regards, Hans Vogelaar

    Friday, May 11, 2012 3:13 PM
  • Thanks Hans..Now the number of paragraphs that I am getting using the above code in QTP is matching with the number of paragraphs that we get by pressing word count button in review tab in word document

    Can you please let me know if this count includes empty paragraphs also?


    Vijay

    Friday, May 11, 2012 3:17 PM
  • This count does *not* include empty paragraphs.

    Regards, Hans Vogelaar

    Friday, May 11, 2012 3:31 PM
  • Do you know any code to get the empty paragraphs in word document so that I can verify number of paragraphs with my piece of code (wrddoc.paragraphs.count) as I suspect that this includes empty paragraphs also?

    Also, this issue is also happening with number of characters. How can that be possible?


    Vijay

    Friday, May 11, 2012 3:46 PM
  • I think you'd have to loop through the paragraphs, for example:

    Function EmptyParas(wrdDoc)
        For Each para In wrdDoc.Paragraphs
            If Len(para.Range.Text) = 1 Then
                n = n + 1
            End If
        Next para
        EmptyParas = n
    End Function
    ActiveDocument.Characters.Count includes end-of-paragraph markers and end-of-cell markers as characters. The Word Count dialog doesn't.

    Regards, Hans Vogelaar

    Friday, May 11, 2012 4:15 PM
  • Hans,

    This issue may not be due to empty paragraph:

    I am getting 172 using wrdDoc.paragraphs.count

    whereas I am getting 131 by pressing word count button in review tab in word document

    That means, there should be 41 empty paragraphs


    but by running above code, I am getting only 3 empty paragraphs....can you please check?


    Also, to check for empty paragraphs, you used len(para.range.text)=1...shouldn't this be zero instead of 1?


    Vijay

    Friday, May 11, 2012 4:29 PM
  •  The difference may be caused by paragraphs that are not truly empty, but that consist of nothing but spaces as those are also ignored by the Word Count dialogue..

    To check, try this instead:

    Len(Trim(para.Range.Text))

    And, yes, "Len = 1" is correct as a paragraph must contain at least the paragraph marker, i.e. the ¶ (or the end of cell marker: ¤) that you can see when you display all characters, and that counts a character in the paragraph string.

    Friday, May 11, 2012 5:38 PM
  • Empty cells in tables ALSO count as paragraphs..because they are.  Are there tables in you document?

    Word MVP

    Friday, May 11, 2012 6:45 PM
  • Word's statistics dialogue also doesn't count paragraphs formatted as hidden, unless Word is configured to print hidden text. Likewise, the dialogue's count can be affected by whether you've checked the option to include textboxes, endnotes and footnotes. Even then it doesn't count anything in headers or footers and field codes containing paragraph breaks can also throw the count off (even if the paragraphs aren't displayed).


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Friday, May 11, 2012 10:47 PM
  • @ fumei - yes, there are tables in the document and when I deleted the tables from the document, then

    number of para by word count dialog + number of para by empty para code(given by hans above) = number of para by qtp code (wrdDoc.paragraphs.count)

    Is there any way to count empty cells in tables as they also count as empty paragraphs?

    Also, is there any way to count end-of-paragraph markers and end-of-cell markers as this issue is happening for character count?

    Vijay

    Wednesday, May 16, 2012 9:49 AM
  • The following function counts the number of times the specified character occurs in (the body of) the active document:

    Function CountChar(c As String)
        Dim s As String
        Dim p As Long
        Dim q As Long
        s = ActiveDocument.Range.Text
        p = Len(s)
        q = Len(Replace(s, c, ""))
        CountChar = p - q
    End Function

    Here is an example of its use:

    Sub Demo()
        Dim n As Long
        ' Paragraph marks
        n = CountChar(vbCr)
        MsgBox "The total number of end-of-paragraph marks is " & n
        ' Cell markers
        n = CountChar(Chr(7))
        MsgBox "The total number of end-of-cell markers is " & n
    End Sub


    Regards, Hans Vogelaar

    • Marked as answer by Dummy yoyo Friday, May 25, 2012 7:44 AM
    Wednesday, May 16, 2012 10:25 AM