We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Inserting merge fields

Hello,


I’m pretty new to these forums because we as a company recently purchased aspose words for .net and I must say it’s pretty impressive.

The reason I came to here is just to clear out 1 small question regarding the usage of merge fields in word files.

If I understood correctly and correctly understood the current existing documentation it is :

- Possible to automatically fill in the merge fields from the data source you are providing to the mailmerge
- Add multiple types of data sources, either XML , an ADO.net source, an data table/view and so on.

What I currently want to do is the following

I’m creating a simple word document to send invoices to our customers.
My invoice object that I want to pass to my word file has the following properties
- An invoicenumber
- An invoicedate
- A customernumber

If I start from a new word template, then I’d have to add these merge fields manually before running the aspose mailmerge.
Is it possible that I can add those merge fields automatically with aspose?
I’ve tried multiple methods to add the merge fields and then save my word file, but everytime I open up the word file than “Insert Merge Field” button is greyed out in word.

I’ve added a screenshot to show what I mean.


Hi,


Thanks for your inquiry. There are two ways you can use to insert merge field in document:

1) Using DocumentBuilder.InsertField method as follows:

Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);<o:p></o:p>

builder.InsertField(" MERGEFIELD mf ");<o:p></o:p>

doc.Save(MyDir + @“16.2.0.docx”);

2) Using DOM class FieldMergeField:

Document doc = new Document();

Paragraph para = doc.FirstSection.Body.FirstParagraph;<o:p></o:p>

FieldMergeField mergeField = (FieldMergeField)para.AppendField(FieldType.FieldMergeField, false);<o:p></o:p>

mergeField.FieldName = “mf”;<o:p></o:p>

doc.Save(MyDir + @“16.2.0.docx”);

Hope, this helps.

Best regards,