I'm trying to add several empty fields (i.e ctrl+F9
shortcut) trough a loop to a word document (via VSTO) but for some reasons only the last one appears in the document.
Can anyone help me ?
Here is the code I use :
Document doc = Globals.ThisAddIn.Application.ActiveDocument;
if (doc == null)
return;
for (int i = 0; i < 5; i++)
{
Paragraph para = doc.Paragraphs.Add();
object fieldType = WdFieldType.wdFieldEmpty;
object text = "test" + i;
object preserve = false;
Field f = doc.Fields.Add(para.Range , ref fieldType, ref text, ref preserve);
}
The output is :
{ test4 }