C# docX ReplaceText工作不正确

C# docX ReplaceText工作不正确,c#,ms-word,C#,Ms Word,我有一个模板: 1. Q1 a. Q1a b. Q1b c. Q1c d. Q1d 。 . 我想用一些数据替换Q1-Q10。我使用DocX库 for (int q = 0; q < 20; q++) { docX.ReplaceText(String.Format("Q{0}", q+1), questions1[q].Text, false, RegexOptions.ExplicitCapture); d

我有一个模板:

1.  Q1
 a. Q1a
 b. Q1b
 c. Q1c
 d. Q1d
。 .

我想用一些数据替换Q1-Q10。我使用DocX库

  for (int q = 0; q < 20; q++)
        {
            docX.ReplaceText(String.Format("Q{0}", q+1), questions1[q].Text, false, RegexOptions.ExplicitCapture);
            docX.ReplaceText(String.Format("Q{0}a", q+1), questions1[q].AnswerA, false, RegexOptions.ExplicitCapture);
            docX.ReplaceText(String.Format("Q{0}b", q+1), questions1[q].AnswerB, false, RegexOptions.ExplicitCapture);
            docX.ReplaceText(String.Format("Q{0}c", q+1), questions1[q].AnswerC, false, RegexOptions.ExplicitCapture);
            docX.ReplaceText(String.Format("Q{0}d", q+1), questions1[q].AnswerD, false, RegexOptions.ExplicitCapture);
        }
但它就像: 因此,输出应针对9个以上的问题:

     2.  This is 10 question
     a. This is 10a question
     b. This is 10b question
     c. This is 10c question
     d. This is 10d question
     2.  This is 1 question0
      a.    This is 1a question0
      b.    This is 1b question0
      c.    This is 1c question0
      d.    This is 1d question0
所以我假设ReplaceText(src,dst)搜索包含src的块,并立即用dst替换。如何使其搜索精确值


谢谢。

所以这个DocX库不能很好地完成这个任务,所以我使用了以下代码:

private void FindAndReplace(Microsoft.Office.Interop.Word.Application doc, object findText, object replaceWithText)
    {
        //options
        object matchCase = false;
        object matchWholeWord = true;
        object matchWildCards = false;
        object matchSoundsLike = false;
        object matchAllWordForms = false;
        object forward = true;
        object format = false;
        object matchKashida = false;
        object matchDiacritics = false;
        object matchAlefHamza = false;
        object matchControl = false;
        object read_only = false;
        object visible = true;
        object replace = 2;
        object wrap = 1;
        //execute find and replace
        doc.Selection.Find.Execute(ref findText, ref matchCase, ref matchWholeWord,
            ref matchWildCards, ref matchSoundsLike, ref matchAllWordForms, ref forward, ref wrap, ref format, ref replaceWithText, ref replace,
            ref matchKashida, ref matchDiacritics, ref matchAlefHamza, ref matchControl);
    }

对象匹配wholeword=true//这一行是关键的

请提供一个指向您正在使用的库以及您的问题所涉及的库的链接。替换文本正是您告诉它要做的事情。如果用
Test
替换字符串
Q1,Q15a
Q1
的所有实例,则得到
Test,Test5a
。字符串替换可能是一个错误的选择。有什么原因不能使用邮件合并字段吗@stuartd@theB我有一个大文件与Q1。。。作为模板,所以我需要插入数据而不是每个Qn。替换方法应至少计算要替换的字符数,并且仅当该数字等于找到的字符数时才进行替换。这一定是一种选择。
private void FindAndReplace(Microsoft.Office.Interop.Word.Application doc, object findText, object replaceWithText)
    {
        //options
        object matchCase = false;
        object matchWholeWord = true;
        object matchWildCards = false;
        object matchSoundsLike = false;
        object matchAllWordForms = false;
        object forward = true;
        object format = false;
        object matchKashida = false;
        object matchDiacritics = false;
        object matchAlefHamza = false;
        object matchControl = false;
        object read_only = false;
        object visible = true;
        object replace = 2;
        object wrap = 1;
        //execute find and replace
        doc.Selection.Find.Execute(ref findText, ref matchCase, ref matchWholeWord,
            ref matchWildCards, ref matchSoundsLike, ref matchAllWordForms, ref forward, ref wrap, ref format, ref replaceWithText, ref replace,
            ref matchKashida, ref matchDiacritics, ref matchAlefHamza, ref matchControl);
    }