與合併字段中的所有文本我有兩個「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