使用Microsoft.Office.Interop.Word
DLL我想找到特定的段落,并在旧段落下方的文档中重复该段落。在这里,我能够使用给定的string (or) text
从所选文档中提取段落。
以下是代码块:
private string[] ReadFileContent(Document doc, string Key)
{
Word::Application WordApp;
WordApp = new Word.Application();
List<string> SelectedParagraphs = new List<string>();
string parac = string.Empty;
for (int j = 1; j <= doc.Paragraphs.Count; j++) //numeration of paragraphs starts from 1
{
Microsoft.Office.Interop.Word.Paragraph para = doc.Content.Paragraphs[j];
int cSent = para.Range.Sentences.Count;
string a;
for (int l = 1; l <= cSent; l++)
{
Microsoft.Office.Interop.Word.Range sent = para.Range.Sentences[l];
a = sent.Text.ToString();
if (a.IndexOf(Key)>-1)
{
parac = parac+ "," + j.ToString();
}
}
}
parac = parac.TrimStart(',');
string[] ParaArray = parac.Split(',');
string[] Result = new string[ParaArray.Count()];
for (int m = 0; m < ParaArray.Count(); m++)
{
int i = 0;
//StringBuilder sb = new StringBuilder();
Microsoft.Office.Interop.Word.Paragraphs DocPar = doc.Paragraphs;
// Count number of paragraphs in the file
long parCount = DocPar.Count;
// Step through the paragraphs
int paraGraphNum = Convert.ToInt32(ParaArray[m]);
while (i < parCount)
{
i++;
if (i == paraGraphNum)
{
// sb.Append(DocPar[i].Range.Text);
Result[m] = DocPar[i].Range.Text;
break;
}
}
}
return Result;
}