Update Date field with current date

Hello there,

I have a field like following in my source document:

{ DATE \@ “d MMMM yyyy” \* MERGEFORMAT}

How can I update this field while doing MailMerge?

Thank you!

Hi

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:
    https://reference.aspose.com/words/net/aspose.words/document/updatefields/

Also please see the following link to learn what fields are supported:
https://reference.aspose.com/words/net/aspose.words/document/updatefields/
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 {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

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

Thank you for additional information. You can find the answer here:
https://forum.aspose.com/t/81635
Best regards.

Thank you :slight_smile: