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

Free Support Forum - aspose.com

V10 Allowing users to move/insert pre-existing mailmerge fields added programatically

Hi there,

My application creates a blank template document with mailmerge fields added programatically using the document builder having a {MERGEFIELD field} format based on rules in our business objects.
The intention is that users can then download a blank template, add these (pre-defined fields) to the page and then move around and place them where is appropriate before uploading the template back to our application.
For example, a letter when printed may have a standard text body and several dynamic calculated figures along with an address, barcode and other custom fields.
When a document is needed to be printed, the arranged document is then merged using code and all the values going into the spaces that the users have configured for them to go.
However, whilst I can add the fields easily enough with the document builder, I cannot see them in a list that would be available to the user whilst in Word so they can arrange them appropriately.
Am I missing something or is it not possible to pre-define a set of fields and have the users see and re-arrange them within word?
Thanks
Jon


This message was posted using Page2Forum from Aspose.Words for .NET - Documentation

Hi Jon,

Thanks for your request. To achieve this You should specify mail merge data source. Please see the following link for more information:

http://www.aspose.com/documentation/.net-components/aspose.words-for-.net/aspose.words.document.mailmergesettings.html

For example, see the following code:

string dataSource = @"C:\Temp\test.csv";

Document doc = new Document();

doc.MailMergeSettings.DataType = MailMergeDataType.TextFile;

doc.MailMergeSettings.MainDocumentType = MailMergeMainDocumentType.FormLetters;

doc.MailMergeSettings.DataSource = dataSource;

doc.MailMergeSettings.Query = string.Format(@"SELECT * FROM {0}", dataSource);

Regex whiteSpaceRegex = new Regex(@"\s+");

using (StreamReader reader = new StreamReader(dataSource, Encoding.UTF8))

{

string firstLine = reader.ReadLine();

string[] fieldNames = firstLine.Split(new char[] {','}, StringSplitOptions.RemoveEmptyEntries);

for (int i = 0; i < fieldNames.Length; i++)

{

OdsoFieldMapData mapData = new OdsoFieldMapData();

mapData = new OdsoFieldMapData();

mapData.Column = i;

mapData.MappedName = fieldNames[i];

mapData.Name = whiteSpaceRegex.Replace(fieldNames[i], "_");

mapData.Type = OdsoFieldMappingType.Column;

doc.MailMergeSettings.Odso.FieldMapDatas.Add(mapData);

}

}

doc.Save(@"Test001\out.doc");

Datasouce is txt file, which contains data in the following format:

Field1,Field2,Field3,Field4,

, , , ,

In this case you will be able to insert predefined merge field into your template. Please see the attached screenshot.

However, you should note that in this case, the data source should be also available on the end user PC.

Best regards.

Hi there,

This worked in the end to reproduce the following sequence in Word:-

-> Insert->Field->Mail Merge->MergeField->TypeMyfieldNameHere

At that point I have a nice little field (without having set a datatable) called <> which I can then move around (with it’s grey background) and copy/paste in my document.

Using the doc builder to achieve the same effect:-

Document myDoc = new Document(@“C:\Templates\Blank\BlankMaster.doc”);
DocumentBuilder builder = new DocumentBuilder(myDoc);
builder.InsertField(“MERGEFIELD test2”);
builder.InsertField(“MERGEFIELD test3”);
builder.InsertField(“MERGEFIELD test4”);
builder.InsertField(“MERGEFIELD test5”);
myDoc.Save(@“c:\temp.doc”,Aspose.Words.SaveFormat.Doc);

Now my users can edit the templates to their hearts content!
:slight_smile:

Hi

Thank you for additional information. Now I got your point. You do not see merge fields in the output document because you do not set field value. Just modify your code as shown below:

Document myDoc = new Document(@"Test001\in.doc");

DocumentBuilder builder = new DocumentBuilder(myDoc);

string[] names = { "test1", "test2", "test3", "test4", "test5" };

foreach (string name in names)

{

builder.InsertField(string.Format("MERGEFIELD {0}", name), string.Format("«{0}»", name));

}

myDoc.Save(@"Test001\out.doc");

Hope this helps.

Best regards,