Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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
Updated 27-Mar-17 19:34pm

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:

C#
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:
C#
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
 
Share this answer
 
v4
//Add Library
using Word = Microsoft.Office.Interop.Word;

try
{
object oMissing = System.Type.Missing;
            Word.Application wordApp = new Word.Application();
            Microsoft.Office.Interop.Word.Paragraphs paragraphs = null;

            Microsoft.Office.Interop.Word.Document doc = wordApp.Documents.Open(ref wordPath, ref oMissing,
                 ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                 ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                 ref oMissing, ref oMissing, ref oMissing, ref oMissing);
            doc.Activate();
 //To delete empty paragraph
                paragraphs = doc.Paragraphs;
foreach (Word.Paragraph paragraph in paragraphs)
                    {
                        if (paragraph.Range.Text.Trim() == string.Empty)
                        {
                            paragraph.Range.Select();
                            wordApp.Selection.Delete();
                        }
                    }
}catch (Exception)
            {
            }
            finally
            {
                if (doc != null)
                {
                    ((_Document)doc).Close(ref oMissing, ref oMissing, ref oMissing);
                    Marshal.FinalReleaseComObject(doc);
                }
                if (wordApp != null)
                {
                    ((_Application)wordApp).Quit();
                    Marshal.FinalReleaseComObject(wordApp);
                }
            }
 
Share this answer
 
v2
Comments
CHill60 28-Mar-17 7:10am    
The question was asked, answered and solution accepted nearly 6 years ago.
Whilst you have presented yet another way of determining if the paragraph is empty you haven't included any words to draw our attention to it.
Worse than that you have an empty catch block that is "swallowing" every exception - this is very bad practice

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900