Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# C#4.0 Word
Hi All,
 
I want to detect Empty paragraphs in Word Document using Microsoft.Office.Interop.Word. Suppose, if my word document have some empty paragraphs,then
 
Assume paragraph 3 is an empty paragraph...
 
Microsoft.Office.Interop.Word.Paragraph para = wordDoc.Content.Paragraphs[3];
int cSent = para.Range.Sentences.Count;
 
for (int j = 1; j <= cSent; j++)
{
 Microsoft.Office.Interop.Word.Range sent = para.Range.Sentences[j];
 MessageBox.Show("Sent lines :" + sent.Text.ToString());
}
Then empty paragraphs has taken the last sentence of the last non-empty paragraph.So, I can't able to detect empty paragraphs in my Word Document.
 
Is there a way to get Empty paragraph list?
 
Please Guide me to Get out of this problem...
Posted 2-Jun-11 5:00am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You're doing it wrong. You assume 3 empty paragraphs (why? you need to detect them all), but you take one paragraph #3 instead. Why?!
 
Take all paragraphs and enumerate them:
 
using Microsoft.Office.Interop.Word;
using ParagraphsList =
    System.Collections.Generic.List<Microsoft.Office.Interop.Word.Paragraph>;
 
//...

static Paragraph[] FindEmptyParagraphs(Document document) {
    ParagraphsList list = new ParagraphsList();
    foreach (Paragraph para in document.ListParagraphs)
        if ((para.Range.End - para.Range.Start) < 1)
            list.Add(para);
    return list.ToArray();
}
 
a different way:
using Microsoft.Office.Interop.Word;
using ParagraphsList =
    System.Collections.Generic.List<Microsoft.Office.Interop.Word.Paragraph>;
 
//...

static Paragraph[] FindEmptyParagraphs(Document document) {
    ParagraphsList list = new ParagraphsList();
    foreach (Paragraph para in document.Content.Paragraphs)
        if ((para.Range.End - para.Range.Start) < 1)
            list.Add(para);
    return list.ToArray();
}
 
Done!
 
—SA
  Permalink  
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web02 | 2.8.1411022.1 | Last Updated 6 Jun 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100