本文介绍了如何在C#4.0中使用Microsoft.Office.Interop.Word在Word文档中检测空段落?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Microsoft.Office.Interop.Word检测Word文档中的空段落。
假设,如果我的word文档有一些空段落,那么

I want to detect Empty paragraphs in Word Document using Microsoft.Office.Interop.Word.Suppose, if my word document have some empty paragraphs,then

假设段落3是一个空段落...

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());
}

然后空段落取最后一个非空段落的最后一个句子。因此,我无法检测到我的Word文档中的空段落。

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...

推荐答案

第二,类似这样的工作应该可以工作

Second, something like this should work

for each p in Doc.Content.Paragraphs
    if (p.Range.End - p.Range.Start) > 1 then (The paragraph is not empty)
Next

您可能需要1数字,因为我不记得Word设置开始和结束点,空段落从开始到结束可能是2个字符长,而不只是一个。

You may need to play with that "1" number, because I can't recall where Word sets the start and end points, empty paragraphs may be 2 chars long from start to end, not just one.

您也可以执行

p.Range.Sentences.Count > 0

p.Range.Characters.Count > 0

但这些技巧通常比检查开始和结束位置要慢。

But those techniques are typically slower than checking the start and end positions.

这篇关于如何在C#4.0中使用Microsoft.Office.Interop.Word在Word文档中检测空段落?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 07:55