我有一个模板:
1. Q1
a. Q1a
b. Q1b
c. Q1c
d. Q1d
。 。
2. Q10
a. Q10a
b. Q10b
c. Q10c
d. Q10d
我想用一些数据替换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
但它就像: 所以输出应该是9个以上的问题:
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的块,并且immidiatelly替换为dst。如何使其搜索EXACT值。
感谢。
答案 0 :(得分:1)
所以这个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);
}
object matchWholeWord = true; //这一行很重要