Malick
Malick

Reputation: 6742

Add empty fields to a word document via C# Interop

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 } 

MSDN Reference link here

Upvotes: 0

Views: 1573

Answers (4)

Somdip Dey
Somdip Dey

Reputation: 3386

if you want to show all the text objects in the loop then try this instead....`

    object text = "";
    for (int i = 0; i < 5; i++)
    {
        Paragraph para = doc.Paragraphs.Add();
        object fieldType = WdFieldType.wdFieldEmpty;
        if(text.toString() != "")
        text = text + "test" + i;
        else
        text = "test" + i;
        object preserve = false;
    }
    Field f = doc.Fields.Add(para.Range , ref fieldType, ref text, ref preserve);

Output: { test1 test2 test3 test4 }

Or you can also try using Field.Update() strategically to wherever needed in order to update the fields being populated. Some times its the issue with buffering.

foreach (Field field in doc.Fields)
field.Update();

So you may try....

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);
        foreach (Field field in doc.Fields)
          field.Update();
    }

Upvotes: 1

gandalf
gandalf

Reputation: 470

concat Environment.NewLine to your text inside the loop

Upvotes: 0

Hans Passant
Hans Passant

Reputation: 942247

It works just fine, but hard to see. Problem is that all 5 fields are on top of each other. Perhaps induced by stopping to soon and it is not clear how you want to proceed. Arbitrarily, append this code to the for-loop body to make the fields more visible:

f.ShowCodes = true;
para.Range.InsertAfter("\r\n");

Upvotes: 2

George Vovos
George Vovos

Reputation: 7618

That's because every paragraph overrides the previous one.
The following example creates the new paragraph at the begging of the document

using Microsoft.Office.Interop.Word;

namespace ConsoleWord
{
    class Program
    {
        static void Main(string[] args)
        {
            Document doc = new Document();
            for (int i = 0; i < 5; i++)
            {
               var  range = doc.Range(0, 0);

                object text = "test" + i;
                object fieldType = WdFieldType.wdFieldAuthor;

                Paragraph para = doc.Paragraphs.Add(range);
                doc.Fields.Add(range, ref fieldType, ref text);

            }
            doc.SaveAs2(@"C:\tmpc\aa.docx");
        }
    }
}

Upvotes: 1

Related Questions