Free Support Forum - aspose.com

How to open a mail merge document without executing the mail merge function

Hi,

I'm evaluating Aspose.Words for .NET and I was wondering if it is possible to "prepare" a document with mail merge functionality - but without executing the mail merge itself - and open the document so that the user can add additional text lines into the document and then finish the mail merge.

Thanks,

Daniel

Hi

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your request. Sure you can achieve this using Aspose.Words. You should just specify mail merge data source. Please see the following link for more information:

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

For example, see the following code:

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

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,

, , , ,

Also, if you would like to create template from scratch, you should insert mergefields into your document. You can achieve this using DocumentBuilder. Please see the following link for more information.

http://www.aspose.com/documentation/.net-components/aspose.words-for-.net-and-java/aspose.words.documentbuilder.insertfield_overload_1.html

Hope this helps.

Best regards.

Hi,

Thanks for your answer.

I hope my following question doesn't look too stupid to you because neither my .NET- nor my Aspose.Words know-how (nor my English) is great.

I've already programmed a DataTable for my document, so isn't it possible to use it?:

System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(ConnectionString);
string commandString = "pr_GetContactIds";
System.Data.SqlClient.SqlCommand command = new SqlCommand(commandString, conn);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.AddWithValue("OfferId", Guid);
command.Parameters.AddWithValue("ContactIds", Guids);
command.Parameters.AddWithValue("ReportType", "Registration");
DataSet ds = Data.DataProvider.GetDataSet(command);

DataTable table = new DataTable();
if (ds.Tables[0].Rows.Count > 0)
{
System.Data.SqlClient.SqlConnection conn2 = new System.Data.SqlClient.SqlConnection(ConnectionString);
string commandString2 = "pr_CourseConfirmation";
System.Data.SqlClient.SqlCommand command2 = new SqlCommand(commandString2, conn2);
command2.CommandType = CommandType.StoredProcedure;
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
command2.Parameters.Clear();
command2.Parameters.AddWithValue("OrderId", (String)ds.Tables[0].Rows[i][0]);
command2.Parameters.AddWithValue("ContactId", (String)ds.Tables[0].Rows[i][1]);
command2.Parameters.AddWithValue("ElementId", "24");
command2.Parameters.AddWithValue("ReceiverContactId", (String)ds.Tables[0].Rows[i][3]);
DataSet ds2 = Data.DataProvider.GetDataSet(command2);
if (ds2.Tables[0].Rows.Count > 0)
{
if (table.Rows.Count == 0 && table.Columns.Count == 0)
{
table = ds2.Tables[0];
}
else
{
table.Rows.Add(ds2.Tables[0].Rows[0].ItemArray);
}
}
}
}
Document doc = new Document(@"Test.doc");
//doc.MailMerge.Execute(table);
doc.Save("Test.doc", SaveFormat.Doc, SaveType.OpenInApplication, Response);

Regards

Hi

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. You can use DataTable to execute mail merge as described here:

http://www.aspose.com/documentation/.net-components/aspose.words-for-.net-and-java/howto-execute-simple-mail-merge.html

or here:

http://www.aspose.com/documentation/.net-components/aspose.words-for-.net-and-java/howto-execute-simple-mail-merge.html

But you cannot connect the document to DataTable. As a data source, you can use for example MS Access database. Please see MergeDataType enum for more information:

http://www.aspose.com/documentation/.net-components/aspose.words-for-.net-and-java/aspose.words.settings.mailmergedatatype.html

Best regards.

Hi,

thanks for your answer. It is working now as you described it above.

When I open the document like this:

doc.Save("Test.doc", SaveFormat.Doc, SaveType.OpenInApplication, Response);

there is always the question about opening the document with the following select statement (YES/NO). Is there a possibility to set the answer by default on YES and skip the question?

Regards

Daniel

Hi

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your request. There is no way to achieve this using Aspose.Words. This issue is not related to Aspose.Words, this is related to browser specific.

Best regards,

Hi,

Thanks for your answer.

I have another problem. I use Aspose.Words in a web application, therefore I generate the document on the server and send it to the user via browser like this:

doc.Save("Test.doc", SaveFormat.Doc, SaveType.OpenInApplication, Response);

My problem now is that when the user opens the document, the question about opening the document with the following select statement (YES/NO) refers to the textfile on the server. Therefore, Word can't find the data source. Is there a way to put the data into the document or what is a common way to solve this problem? In my opinion, using a web application you always have this problem, haven't you...?

Regards

Daniel

Hi

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thank you for additional information. I think in your case, you can use MailMerge to fill your document with data and then save this document. Please see the following link to learn more:

http://www.aspose.com/documentation/.net-components/aspose.words-for-.net-and-java/simple-mail-merge-explained.html

Please let me know in case of any issues.

Best regards,

Hi,

Thanks for your answer. But I don't think this is the solution for my problem. When I either save or open the word document on the client, I still don't have a connection to the file system on the server (where my application is deployed). As you can see in the attachment, the path refers to c:\mailmerge\mailmerge.txt on the server. Is there no way to put the data into the word document (without executing the mail merge function)...?

Regards

Daniel

Hi

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thank you for additional information. There is no way to put this data into the document. But I still cannot understand the reason why do you need to send this data source to client. Anyway on the client side your document will be filled with data, but you can do it on server side. If it is really necessary you can try zipping both of files (document and data) and send to client.

Best regards,