如何在单词2010中找到段落的索引

时间:2014-05-15 06:06:46

标签: c# asp.net office-addins word-2010

我正在阅读c#中的word文档,在阅读之后,我需要为所选段落输入注释。

所以我需要通过c#找到段落的索引,是否可能?

foreach (Microsoft.Office.Interop.Word.Paragraph aPar in oDoc.Paragraphs) // looping through all the paragh in document
{
   Microsoft.Office.Interop.Word.Range parRng = aPar.Range;
   string sText = parRng.Text;
   if (sText == para[1].ToString())   // found the paragraph and i need the index of this paragraph
   {
       oDoc.Comments.Add(oDoc.Paragraphs[0].Range, ref comments); // to add the comment in document
   }
} 

如果我找到该段落的索引,我可以在该段落上插入评论吗?有可能吗?

或者还有其他方法吗?

2 个答案:

答案 0 :(得分:0)

试试这个

    foreach (Microsoft.Office.Interop.Word.Paragraph aPar in oDoc.Paragraphs) // loads all words in document
    {
        Microsoft.Office.Interop.Word.Range parRng = aPar.Range;
        string sText = parRng.Text.Replace("\r","");
        if (sText == txtBoxParagraph.Text )   // found the paragraph and i need the index of this paragraph
        {
            oDoc.Comments.Add(parRng, txtBoxComments.Text);   // to add the comment in document
        }
    }

它对我有用。

答案 1 :(得分:0)

              int i = 1;
              foreach (Word.Paragraph aPar in oDoc.Paragraphs)
              {
              string sText = aPar.Range.Text;
             if (sText != "\r")
             {
               if (sText == para[1].ToString() + "\r")
               {
               Word.Range range = oDoc.Paragraphs[i + 1].Range;
               if (!range.Text.Contains("GUID:"))
                   {
                       int pEnd = aPar.Range.End;
                       string guid = "GUID:" + para[0].Replace("{", "").Replace("}", "");
                       int length = guid.Length;
                       aPar.Range.InsertAfter(guid);
                       Word.Range parRng = oDoc.Range(pEnd, pEnd + length);
                       parRng.Font.Hidden = 1;
                       parRng.InsertParagraphAfter();
                       }
                     }
                   }
                   i++;
                 }