none
Line Count Without Blank Lines RRS feed

  • Question

  • There exists a way to count characters without spaces (Word.WdStatistic.wdStatisticCharacters) and with spaces(Word.WdStatistic.wdStatisticCharactersWithSpaces).

    Is there a way to count the number of Lines excluding blank lines?  Thank you

    Thursday, March 17, 2011 1:41 AM

Answers

  • I discovered that the paragraphs collection also gives blank lines. I had thought the collection would only contain lines with text, but it includes both. So I modified my code to be: (just need an elegant way to identify lines with multiple line spaces followed by the \r)

    int totalLineCount = 0; 

    int totalWordCount = 0; 

    int totalCharxCount = 0; 

    int totalCharsCount = 0;

                            

    foreach (Word.Range range in document.StoryRanges)

    {

      foreach (Word.Paragraph para in range.Paragraphs)

      {

         if (para.Range.Text == "\r")  // remove blank lines

             para.Range.Text = "";  

         if (String.Empty != range.Text.Trim())

         {

           totalLineCount += range.ComputeStatistics(Word.WdStatistic.wdStatisticLines);

           totalWordCount += range.ComputeStatistics(Word.WdStatistic.wdStatisticWords);

           totalCharxCount += range.ComputeStatistics(Word.WdStatistic.wdStatisticCharacters);

           totalCharsCount += range.ComputeStatistics(Word.WdStatistic.wdStatisticCharactersWithSpaces);

         }

      }

    }

     

    Wednesday, March 23, 2011 7:09 PM

All replies

  • Hi OfficeNewBe,

    Thanks for posting in the MSDN Forum.

    I think you can get the text from the range and count the characters. However I would not recommend you do that.

    And we can count lines via ComputeStatistics method and use the Word.WdStatisticLines.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, March 18, 2011 3:05 AM
    Moderator
  • My main question is how to ignore blank lines.  The following code counts the lines for each story part.  However the line count includes blank lines.  Is there a way to eliminate the blank lines from the count?  Thanks

    foreach (Word.Range range in document.StoryRanges)

    {

      if (String.Empty != range.Text.Trim())

      {

                        totalLineCount += range.ComputeStatistics(Word.WdStatistic.wdStatisticLines);

      }

    }

     

    Monday, March 21, 2011 2:52 AM
  • I discovered that the paragraphs collection also gives blank lines. I had thought the collection would only contain lines with text, but it includes both. So I modified my code to be: (just need an elegant way to identify lines with multiple line spaces followed by the \r)

    int totalLineCount = 0; 

    int totalWordCount = 0; 

    int totalCharxCount = 0; 

    int totalCharsCount = 0;

                            

    foreach (Word.Range range in document.StoryRanges)

    {

      foreach (Word.Paragraph para in range.Paragraphs)

      {

         if (para.Range.Text == "\r")  // remove blank lines

             para.Range.Text = "";  

         if (String.Empty != range.Text.Trim())

         {

           totalLineCount += range.ComputeStatistics(Word.WdStatistic.wdStatisticLines);

           totalWordCount += range.ComputeStatistics(Word.WdStatistic.wdStatisticWords);

           totalCharxCount += range.ComputeStatistics(Word.WdStatistic.wdStatisticCharacters);

           totalCharsCount += range.ComputeStatistics(Word.WdStatistic.wdStatisticCharactersWithSpaces);

         }

      }

    }

     

    Wednesday, March 23, 2011 7:09 PM