与合并字段中的所有文本我有两个“DEMO”字我怎么能替换文件
,我想替换成MERGEFIELD
只是一个“DEMO”用我的代码改变...
我怎样才能取代文本与字段?
感谢
Application app = new Application();
Document word = new Document();
word = app.Documents.Add(ref path, ref missing, ref missing, ref missing);
object objType = WdFieldType.wdFieldMergeField;
object hashVal = null;
Hashtable hash = new Hashtable();
hash.Add("DEMO", "mergefield11111");
foreach (object s in hash.Keys)
{
Range rng = app.ActiveDocument.Content;
Find findObject = app.Selection.Find;
object ff = s;
hashVal = hash[s];
findObject.Text = ff.ToString();
findObject.ClearFormatting();
if (rng.Find.Execute(ref ff,
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, WdReplace.wdReplaceOne, ref missing, ref missing,
ref missing, ref missing))
{
Field field = app.Selection.Fields.Add(app.Selection.Range, ref objType, ref hashVal, ref missing);
}
}
app.Documents.Open("test1.docx");
如果你被封锁了,你可以试试我创建的库,就是这样做的(替换值,或循环和条件:https://github.com/open-xml-templating/docxtemplater) – edi9999