Create MailMerge field by Document

Hi.
I see your code sample how to use MailMerge if you have the EXISTING DOCX template with fields inside.

But my task is create mailmerge fields, fill them in, save in docx/pdf as a result.

So, how to create 2 fields, fiil it and save as John, Description by code without any existing Templates.

Thanks

Something like this:

Hi,

Try using these:

...
new Field(document, FieldType.MergeField, "Name", "")
...
new Field(document, FieldType.MergeField, "Description", "")

Does this solve your issue?

Regards,
Mario

Perfect. Works fine and I’m happy

Hey Mario.
Could you show me how to create a table with MergeFields inside by C# code using Gembox.Document?
I’m trying to create a table and insert MergeField with Range inside.

Please how to create the table with fields inside?
P.S. I don’t want to load a docx template, Document can create it on fly?
Thanks

Hi,

Try this:

var document = new DocumentModel();
var section = new Section(document);
document.Sections.Add(section);

var table = new Table(document, 2, 2);
table.TableFormat.PreferredWidth = new TableWidth(100, TableWidthUnit.Percentage);
section.Blocks.Add(table);

table.Rows[0].Cells[0].Blocks.Add(
    new Paragraph(document,
        new Field(document, FieldType.MergeField, "RangeStart:Table1"),
        new Field(document, FieldType.MergeField, "Name")));

table.Rows[1].Cells[0].Blocks.Add(
    new Paragraph(document,
        new Field(document, FieldType.MergeField, "Bla")));

table.Rows[1].Cells[1].Blocks.Add(
    new Paragraph(document,
        new Field(document, FieldType.MergeField, "Description"),
        new Field(document, FieldType.MergeField, "RangeEnd:Table1")));

var source = new
{
    Table1 = new[]
    {
        new { Name = "Name1", Bla = "Bla1", Description = "Description1" },
        new { Name = "Name2", Bla = "Bla2", Description = "Description2" },
        new { Name = "Name3", Bla = "Bla3", Description = "Description3" }
    }
};

document.MailMerge.Execute(source);

document.Save("output.docx");

I hope this helps.

Regards,
Mario

It was perfect. Exactly what I want. Thanks