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

Free Support Forum - aspose.com

Various questions

We are currently using Microsoft word automation to perform server side mailmerges, not ideal for reasons you know as well as it’s very flakey. I downloaded and successfully implemented some basic prototype tests using your ASPOSE.Words Java version, we are ucing coldfusion 9 for our web products. I have a couple of questions that I’m hoping you can tell me if possibly or not and/or give me some hints of how to accomplish.

  1. Currently this is how we execute our mailmerges (we actually are using asp for the mailmerges as CF was not reliable), this is just the relevant code:
WordApp.ActiveDocument.MailMerge.OpenDataSource(DataSourceFile)
with WordApp.ActiveDocument.Mailmerge 
Destination = wdSendToNewDocument
MailAsAttachment = false
MailAddressFieldName = ""
MailSubject = ""
SuppressBlankLines = True
end with
WordApp.ActiveDocument.MailMerge.Execute(true)

Specifically my question is can I use a .txt file as the mailmerge source like I can with our existing word automation with ASPOSE.Words? This is a sample of the .txt file referenced above in DataSourceFile:

CurrentDate|FirstName|LastName|TroopNo|Address|City|State|PostalCode|EventName|EventDate|EventTime|EventDate2|EventTime2|NumOfGirls|GirlFee|NumOfAdults|AdultFee|FeeTotal|CreditType|CreditTotal|TotalDue|Message|EventName2|NumOfGirls2|GirlFee2|NumOfAdults2|AdultFee2|FeeTotal2|TotalDue2|TotalDue3|RegID|StatementID|SiteName|SiteAddress|SiteCity|SiteState|SiteZip|SitePhone|
03/08/2007|AdreAnna|Jones|00713|111 Some Street|Somewhere|CA|46239|Dust Bunny Sculpting|03/31/2007|09:00 AM|03/31/2007|05:00 PM|1|$4.50|1|$8.50|$13.00|No Credits|$0.00|$13.00| |||||||||632|0|Biochemistry Bldg, Purdue University|BCHM|West Lafayette|IN|47907||
03/08/2007|Angela|Black|00045|111 Some Way|Yontvill|CA|46236|Dust Bunny Sculpting|03/31/2007|09:00 AM|03/31/2007|05:00 PM|3|$4.50|1|$8.50|$22.00|No Credits|$0.00|$22.00| |||||||||633|0|Biochemistry Bldg, Purdue University|BCHM|West Lafayette|IN|47907||CurrentDate|FirstName|LastName|TroopNo|Address|City|State|PostalCode|EventName|EventDate|EventTime|EventDate2|EventTime2|NumOfGirls|GirlFee|NumOfAdults|AdultFee|FeeTotal|CreditType|CreditTotal|TotalDue|Message|EventName2|NumOfGirls2|GirlFee2|NumOfAdults2|AdultFee2|FeeTotal2|TotalDue2|TotalDue3|RegID|StatementID|SiteName|SiteAddress|SiteCity|SiteState|SiteZip|SitePhone|
03/08/2007|AdreAnna|Jones|00713|111 Some Street|Somewhere|CA|46239|Dust Bunny Sculpting|03/31/2007|09:00 AM|03/31/2007|05:00 PM|1|$4.50|1|$8.50|$13.00|No Credits|$0.00|$13.00| |||||||||632|0|Biochemistry Bldg, Purdue University|BCHM|West Lafayette|IN|47907||
03/08/2007|Angela|Black|00045|111 Some Way|Yontvill|CA|46236|Dust Bunny Sculpting|03/31/2007|09:00 AM|03/31/2007|05:00 PM|3|$4.50|1|$8.50|$22.00|No Credits|$0.00|$22.00| |||||||||633|0|Biochemistry Bldg, Purdue University|BCHM|West Lafayette|IN|47907||

  1. Is there a way to get rid of the extra text in a merged document created by ASPOSE.Words from a database table, i.e. with this code using your Java component:

I end up with perfectly merged labels, however the ones at the end have the , that existing in the original template (see attached) with no data since there was only XX number of data records in the dataTable query


  1. Is there a way to rename a mergefield name in a document (before a merge actually happens), and related to this if there is would it rename all occurrences of this name (e.g. from <> to <>

Thanks in advance for your help!!

Hi Javier,

Thanks for your inquiry.

Javier Arroyo:

1) Currently this is how we execute our mailmerges (we actually are using asp for the mailmerges as CF was not reliable), this is just the relevant code:

Yes, you can use the Aspose.Words with ASP via COM Interop. Please follow the link to learn how to achieve this:
https://docs.aspose.com/words/net/supported-platforms/#com

Javier Arroyo:

Specifically my question is can I use a .txt file as the mailmerge source like I can with our existing word automation with ASPOSE.Words? This is a sample of the .txt file referenced above in DataSourceFile:

Yes, you can use txt file as datasource. Following C# code example shows how to do Mail Merge with text file data source.

Document doc = new Document(MyDir + "Test12.docx");
string dataSource = MyDir + "MailMerge.txt";
doc.MailMergeSettings.Clear();
doc.MailMergeSettings.DataType = MailMergeDataType.TextFile;
doc.MailMergeSettings.MainDocumentType = MailMergeMainDocumentType.FormLetters;
doc.MailMergeSettings.DataSource = dataSource;
doc.MailMergeSettings.Query = string.Format(@"SELECT  FROM {0}", dataSource);
doc.MailMergeSettings.LinkToQuery = true;
doc.MailMergeSettings.ViewMergedData = true;
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(MyDir + "out.docx");

You need to create a wrapper class in C#/VB.NET if you want to use Aspose.Words for .NET via COM Interop in classic ASP. Please check the detail from following forum link.
https://forum.aspose.com/t/51700

Javier Arroyo:

2) Is there a way to get rid of the extra text in a merged document created by ASPOSE.Words from a database table, i.e. with this code using your Java component:

Please use the MailMerge.setCleanupOptions method to set a set of flags that specify what items should be removed during mail merge. Please check the MailMergeCleanupOptions from here:
https://docs.aspose.com/words/java/clean-up-before-or-during-mail-merge/

Could you please attach your input Word document here for testing? I will investigate the issue on my side and provide you more information.

Javier Arroyo:

3) Is there a way to rename a mergefield name in a document (before a merge actually happens), and related to this if there is would it rename all occurrences of this name (e.g. from <> to <>

Yes, you can rename mail merge fields. I suggest you please read following documentation link for your kind reference.
https://docs.aspose.com/words/java/update-field/

In regards to #1 – I ended up doing something similar to this i.e. reading in .txt file and turning into a “query” in coldfusion, what I was wondering is if apose.Words could simply accept the .txt file as the source-- no worries.

Attached is the word merge source for the labels, lmk how (if) I can accomplish what I want i.e. get rid of the extra “,” in the unused mailing labels after a merge is performed

Hi Javier,

Thanks for your inquiry. In your case, I suggest you please loop through all Paragraph nodes of document and remove the empty Paragraphs and Paragraphs containing only comma (,). Please see the following C# code for your kind reference.

doc.MailMerge.Execute(datatable);
doc.MailMerge.DeleteFields();
foreach (Paragraph para in doc.GetChildNodes(NodeType.Paragraph, true).ToArray())
{
    if (para.ToString(SaveFormat.Text).Trim() == "" || para.ToString(SaveFormat.Text).Trim() == ",")
        para.Remove();
}
doc.Save(MyDir + "out.docx");

Perfect!! …translated the C# code to coldfusion and it worked like a charm!!

Hi Javier,

Thanks for feedback. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.