Click here to Skip to main content
13,046,281 members (120,911 online)
Rate this:
 
Please Sign up or sign in to 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...

<pre>Microsoft.Office.Interop.Word.Paragraph para = wordDoc.Content.Paragraphs[3];<br />
int cSent = para.Range.Sentences.Count;<br />
<br />
for (int j = 1; j <= cSent; j++)<br />
{<br />
 Microsoft.Office.Interop.Word.Range sent = para.Range.Sentences[j];<br />
 MessageBox.Show("Sent lines :" + sent.Text.ToString());<br />
}<br />
</pre>

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 4:00am
Updated 27-Mar-17 19:34pm
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
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

//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);
                }
            }
  Permalink  
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 28 Mar 2017
Copyright © CodeProject, 1999-2017
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