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

Free Support Forum - aspose.com

How create a Word Documents with Merge Mail inside

Hi,


I know how to use merge mail with your api but i don’t know how can i create with your api an empty template with the merge fields ready to be used ?

Can someone help me ?

Regards
Julien

Hi,


Thanks for your request.

You can use DocumentBuilder class to be able to create a template from scratch using Aspose.Words programmatically . Please read the article suggested here:
http://www.aspose.com/documentation/java-components/aspose.words-for-java/com/aspose/words/documentbuilder.html

Also, you can insert a merge field in the document using the following code snippet:

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

DocumentBuilder builder = new DocumentBuilder(doc);

builder.insertField(@"MERGEFIELD MyFieldName \* MERGEFORMAT");

// Insert a simple Date field into the document.

builder.insertField(@"DATE \* MERGEFORMAT");


Moreover, for a list of supported fields, please see the following link:
http://www.aspose.com/documentation/java-components/aspose.words-for-java/about-field-update.html
I hope, this will help.

Best Regards,

Thanks for you answer but it’s not quite what I need.


With this solution, the merge field is include in the document. I need to insert this merge field in the “INSERT MERGE FIELD” function in Word so I can’t put the merge field availabled humanly where i need them.

See ?

In fact, i need to attach a word blank document and a list of merge field who will be available in Word for this document.

Hi

Thanks for your request. You should attach a data source to your document to achieve this. Please see the following thread for more information:

Best regards,

Thanks a lot, i’ll try that !

This code is .Net like, i’ll try to mix it to Java

Ok thanks a lot, it’s working !


When the document is opening, it asking me about the “SELECT * FROM {0}”, I say yes, after, it asking me about the “SELECT * FROM test.txt”, i say yes and after i can use my document.

Questions :
- How can i avoid this ?
- The file datasource, is it mandatory ? How can i inject the merge field in the template without file datasource ?

Here is the Java code for user who will be interested

String dataSource = “D:/test.txt”;

Document doc = new Document();
MailMergeSettings mailMergeSettings = new MailMergeSettings();
mailMergeSettings.setDataType(MailMergeDataType.TEXT_FILE);
mailMergeSettings.setMainDocumentType(MailMergeMainDocumentType.FORM_LETTERS);
mailMergeSettings.setDataSource(dataSource);
mailMergeSettings.setQuery(String.format(“SELECT * FROM {0}”, dataSource));
BufferedReader bufferedReader = new BufferedReader(new FileReader(new File(dataSource)));

String line = bufferedReader.readLine();
StringTokenizer stringTokenizer = new StringTokenizer(line,",");
int column = 0;
OdsoFieldMapDataCollection odsoFieldMapDataCollection = new OdsoFieldMapDataCollection();
while(stringTokenizer.hasMoreTokens()){
String field = stringTokenizer.nextToken();
OdsoFieldMapData mapData = new OdsoFieldMapData();
mapData = new OdsoFieldMapData();
mapData.setColumn(column);
mapData.setMappedName(field);
mapData.setName(field.replaceAll(" ", “_”));
mapData.setType(OdsoFieldMappingType.COLUMN);
odsoFieldMapDataCollection.add(mapData);
column++;
}
Odso odso = new Odso();
odso.setFieldMapDatas(odsoFieldMapDataCollection);
mailMergeSettings.setOdso(odso);

doc.setMailMergeSettings(mailMergeSettings);
doc.save(“D:/out.doc”);

Hi

Thank you for sharing your code. Unfortunately, there is no way to embed data source into the document. MS Word does not allow this. Also I did not manage to find a way to reference data source from web. So the only way to achieve this is using two files – template and data source.

Best regards,

Ok thanks.


And can we avoid the warning message of “SELECT * from {0}” when we open the document ?

Hi

Thanks for your request. I am not sure this is possible in MS Word. You can try setting this option, but I am not sure it will help:

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

Best regards,

Thanks but it doesn’t work.

Hi

Thank you for additional information. I suspected this. Unfortunately, I do not have anything else to suggest. I suppose this is simply impossible in MS Word.

Best regards,