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

Free Support Forum - aspose.com

Update Date field with current date

Hello there,

I have a field like following in my source document:

{<span style=“font-size: 12pt; font-family: “Times New Roman”,“serif”;” lang=“EN-US”>DATE @ “d MMMM yyyy” * MERGEFORMAT}

How can I update this field while doing MailMerge?

Thank you!

Hi

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

Thanks for your inquiry.

1. If you execute simple mail merge (MailMerge.Execute method), then all fields in the document will be automatically updated.

2. If you execute mail merge with regions (MailMerge.ExecuteWithRegions), then all field within the region will be updated automatically.

3. You can update all fields in a document using Document.UpdateFields method:

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

Also please see the following link to learn what fields are supported:

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

Best regards.

Thanks Alexey for your reply.

I tried with both - calling MailMerge.Execute(ds) as well as UpdateFields(),
but it’s not replacing the {<span style=“font-size: 12pt; font-family: “Times New Roman”,“serif”;” lang=“EN-US”>DATE @ “d MMMM yyyy” * MERGEFORMAT} with date value.

However, when I press Alt+F9 manually in any of the source and output document, it does show me the current date.

Source and Output docs are attached for your reference.

Please guide.

Hi

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

Thanks for your request. This works without any issues on my side. Here is simple code, which confirms that updating DATE fields works correctly.

// Create an empty document.

Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

// Insert date field.

builder.InsertField("DATE \\@ \"d MMMM yyyy\"", null);

// Print text of the document.

Console.WriteLine("----====Test of the docuemnt before updating fields===---");

Console.WriteLine(doc.ToTxt());

// Update fields.

doc.UpdateFields();

// Print text of the document after updatign fields.

Console.WriteLine("----====Test of the docuemnt after updating fields===---");

Console.WriteLine(doc.ToTxt());

// Save output document.

doc.Save(@"Test001\out.doc");

Best regards.

Thank you Alexey,

I tried again with exactly your code.

But why I have to press Alt+F9 to see the actual date and not the merge field.
Any idea?

P.S. Attached is the output document being generated on my side.

Hi

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

Thank you for additional information. You can find the answer here:

Best regards.

Thank you :slight_smile: