A date field can be very helpful in mail-merge templates to include the current valid date in an appropriate format.

TX Text Control .NET for Windows Forms provides fully compatible fields to insert and update these date fields. The fields can be accessed through the interface for MS Word fields using the ApplicationField class.

Anyway, the included DocumentServer provides the FieldAdapter class. It implements an adapter for the most commonly used fields like the DATE or MERGEFIELD. In an older post, I explained how to use this adapter class.

Today, I would like to show you how an MS Word compatible DATE field can be created, inserted and updated.

In order to use the AdapterFields, you need to add a reference to the TXDocumentServer.dll. In your Visual Studio 2008 project, click on Project and Add Reference..., select TXTextControl DocumentServer in the .NET tab and confirm with OK.

To add the field, you simply need to create a new DateField using the AdapterField classes:

// [C#]
TXTextControl.DocumentServer.Fields.DateField date =
    new TXTextControl.DocumentServer.Fields.DateField();
date.Date = DateTime.Now;


TX Text Control .NET for Windows Forms doesn't update the field automatically, but provides the interface to update it. Therefore, we can set the Date property of the field to the current date. Finally, the adapter field will be added to the ApplicationField collection of TextControl.

Now, you have to decide when to update the date in the document. For example, you could update the DATE fields when the document is loaded, saved or printed. The following code updates all DATE fields in a document with the current date:

// [C#]
foreach (TXTextControl.ApplicationField field in textControl1.ApplicationFields)
    if (field.TypeName == "DATE")
        TXTextControl.DocumentServer.Fields.DateField curDate =
            new TXTextControl.DocumentServer.Fields.DateField(field);
        curDate.Date = DateTime.Now;

An interesting member of the DATE field is the Format property. It gets and sets the format of the field using the standard DateTime format string of the .NET Framework. The following format would display only the full name of the day:

// [C#]
date.Format = "dddd";

As you can see, using TX Text Control .NET for Windows Forms, you get an easy way to include MS Word-compatible fields. If you export the document to an MS Word format like DOCX, DOC or RTF, these fields and their functionality are maintained and available in MS Word.

You can download the sample project for Visual Studio 2008 here (a TX Text Control .NET for Windows Forms 15.1 trial version is required):