This is a good point. However in Word XP I do not see @ as an available option to specify format for the MERGEFIELD field, it seems to be only available for the DATE field. That’s why we have not implemented support for it. What MS Word version do you use and how did you specify the date format for the field, by manually entering @ …?
There are several possible solutions without using @ format:
Use MergeField event to provide a handler and format the date inside the event handler.
Add one extra calculated column to your table and format the date into a string in the way you want it before mail merge.
Add extra column to your query that produces the data table and format date in the way you need in the query.
To everybody who has the same problem. I post my example and it can compile.
Just remember to change the Connectionstring to the Database and change the SQL call and path.
When du you expect to release a new version whith @ and # options ???
using System;
using System.IO;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Data.OleDb;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Aspose.Word;
namespace Team
{
public class Example : System.Web.UI.Page
{
private string curPath;
private void Page_Load(object sender, System.EventArgs e)
{
this.curPath = MapPath(".");
//Create a document requested by the user. The action is specified in the request string.
Document doc;
string param = this.Request.Params[0];
switch (param)
{
// Write this in your browser (Remember to change the path) participant
case "participant":
doc = CreateParticipant();
break;
// Write this in your browser (Remember to change the path) test
case "test":
doc = Test();
break;
default:
return;
}
//Stream the document to the client browser.
doc.Save(
string.Format("{0}.doc", param),
SaveFormat.FormatDocument,
SaveType.OpenInBrowser, Response);
Response.End();
}
private Document CreateParticipant()
{
DataSet dataSet = new DataSet();
dataSet.Tables.Add(GetParticipant_all());
// Remember to make the document first
Document doc = OpenDoc(@"Afkrydsningsliste3.doc");
doc.MailMerge.ExecuteWithRegions(dataSet);
return doc;
}
/// <summary>
/// Please help me, The code can not compile.
/// </summary>
public Document Test()
{
DataSet dataSet = new DataSet();
dataSet.Tables.Add(GetParticipant\_all());
// Remember to make the document first
Document doc = OpenDoc(@"Afkrydsningsliste3.doc");
doc.MailMerge.MergeField += new MergeFieldEventHandler(HandleFormat);
doc.MailMerge.ExecuteWithRegions(dataSet);
return doc;
}
private void HandleFormat(object sender, MergeFieldEventArgs e)
{
if (e.FieldName == "StartDate")
{
DateTime date = (DateTime)e.FieldValue;
e.Text = date.ToString("d MMMM yyyy");
}
}
///
/// MSSQL SERVER Connection
///
private DataTable ExecuteDataTable(string tableName, string commandText)
{
SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["Connectionstring"]);
SqlCommand myCommand = new SqlCommand(commandText, myConnection);
SqlDataAdapter sd = new SqlDataAdapter(myCommand);
DataTable table = new DataTable(tableName);
sd.Fill(table);
myConnection.Close();
return table;
}
///
/// Just a convenience function to open a document in the predetermined folder.
///
private Document OpenDoc(string fileName)
{
Word app = new Word();
return app.Open(string.Format(@"{0}/…/…/data/report/team/{1}", curPath, fileName));
}
///
/// Returns all participant in my database.
///
private DataTable GetParticipant_all()
{
return ExecuteDataTable("Participant", "SELECT \*, C.Address AS CourseAddress FROM tStmParticipant AS P, tCourse AS C, rCourseParticipant AS CP WHERE CP.ParticipantID = p.ID AND C.ID = CP.CourseID AND P.ActiveStatus = 1");
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the [ASP.NET](http://ASP.NET) Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
We’ve discovered that Word XP will remove all formatting flags it does not understand if you Edit Field and click OK. This behaviour is probably same for all Word versions to strip unknown field flags.
I still plan to support @ and #, but remember that you might lose them if you attempt to edit the document in a previous Word version.
Thanks for your feedback, we would like to have @ and # implemented as this will give more functionality to end user.
However we will keep the issues in mind you have highlighted for us.
Will Aspose.Word deal with formatting commands on text such as bold, italic, and underline.
For instance, if I pass through a sentence in a node in the XML data, and I wanted one of the words in the sentence to be bold, can I enter format commands such as HTML “B” “/B” and will Aspose interpret these correctly in the output document.
We plan to support RTF and HTML documents, but it also seems we will need to support HTML fragments exactly as you describe. Estimated delivery August-September.
In the meantime, if you want to insert formatted text then just use DocumentBuilder.MoveToMergeField to navigate to the field and use Font and ParagraphFormat and DocumentBuilder methods to insert formatted content.
I have been trying to test the formatting commands /@ and /# inside the Mergefields within the template document. I can’t get either currency or date fields formatting.
How did you pass the data from C# through Aspose to the Template document?
I created a XSD for my XML document, defining fieldnames and datatypes. I then read the XSD into my dataset in C# prior to reading the XML document into my dataset.
Once the XSD and the XML doc were both read into the dataset I checked the datatype of each field using GetType, and all fields had been assigned correctly.
I then attempted to merge the fields to the template using Aspose’s DocumentBuilder, but the write command turned all values to strings on output.
builder.Write(row[fieldName].ToString());
I then attempted to merge the fields to the template using Aspose’s ExecuteWithRegions command but this threw an error stating “Specified cast is not valid”
docOutput.MailMerge.ExecuteWithRegions(dset);
Is it possible for me to get hold of the sample code you used to test this.
I see that you say the # and @ formatting tags are supported by Aspose.Word and have been since version 1.5 or so… what about the * formatting tag? In Word you supposedly can use the * Upper setting on a mergefield to make it all uppercase. I have a pretty significant need for this flexibilty for a client I am working on and was wondering if it is supported because I cannot get it to work. After the merge, all fields seem to ignore anything with * formatting options set.
Since the other two formatting options are supported, maybe this one would be easy to add if it is not already in there? Any comments, suggestions you have would be appreciated.
Awesome thanks for the info. I am having problems getting the other formatting items to work by the way. I am trying to format a decimal field using the # switch and some parameters, but I keep getting an exception “Specified Cast is Invalid” while attempting to do the mailmerge. I know for a fact that the value I am trying to format is a decimal (the XML schema has it as xsecimal), so I am not sure what I am doing wrong.
Could you possibly send a word document with the proper field codes in it for formatting a decimal as a currency string? I can send you my Word template and XML if you like as well… my email is mailtopotvader@yahoo.com
But try latest Aspose.Word 1.8.3, I might have it fixed there. There was a cast from object to double, which I replaced with Convert.ToDouble, I think it should help in your case.
Thanks for your help. I upgraded to version 1.8.3 this morning and used your sample mergefield codes and everything is working correctly. I did notice one bit of odd behavior on the part of Word though… it appears to strip out all of your mergefield codes the next time you open the template file in Word. Is this a known bug? I’m using version 2003 SP1.