i have two 'DEMO' word
and i want replace that with mergefield
but just one 'DEMO' was changed using my code...
how can i replace text with field??
thanks
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");