0
votes

Now I can insert a text after the current cursor,using

Globals.ThisAddIn.Application.Selection.Range.Text = itm.Value;

I want to replace the text with a MergeField,but I don't know how to create a MergeField object using VSTO and how to insert it into a document.

2

2 Answers

1
votes

This is how you insert merge field

object objType = Word.WdFieldType.wdFieldMergeField;
object objFieldName = fieldName;
Word.Field field = range.Fields.Add(range, ref objType, ref objFieldName, ref missing);

To replace the text you can either for String.Replace in C# or Find object from Word Object Model.

-1
votes

You could also just add in after the Word.Field field = ....

field.Result.Text = "Whatever you want";