Free Support Forum - aspose.com

Insert SAVEDATE Field in Word Document to Know the Date the Document was Last Saved using C# .NET

See attached sample: when creating a field of type “FieldType.FieldSaveDate” and the document is saved by Aspose.Words, the value is initially “0”. It starts working if the document is edited by word, saved and reopened.
I would expect that is shows the time when “Document.Save” was called.

A call to “Field.Update” or “Document.UpdateFields” does not help.

It gets even worse if you load a template: now the date is the last save time of the template file. Click button “create from template” to reproduce this.

Is this a misunderstanding of mine or a bug?

When creating a blank doc, the user might understand that the “0” values will be filled when first saved. But at least the second problem (create from template) causes real trouble for me, because the customers will see a date in the result file that they don’t understand and that makes no sense to them.

Best regards

Wolfgang

SaveDateTest_Updated.zip (4.7 MB)

@wknauf,

We tested the above scenarios and have managed to reproduce the same problems on our end. For the sake of corrections, we have logged the following issues in our issue tracking system.

WORDSNET-19842: Field “SaveDate” is initially not set
WORDSNET-19843: Field “SaveDate” is not correctly set for Aspose.Words generated Documents

We will further look into the details of these issues and will keep you updated on the status of corrections. We apologize for your inconvenience.

Thanks!

Please keep in mind the locale specific date format - in my sample I set “Document.Styles.DefaultFont.LocaleId” to “German”. I don’t whether this is something that you must handle or whether Word will take care of it.

Wolfgang

@wknauf,

Thanks for the additional information. We have logged these details in our issue tracking system and will inform you when the linked issue will be resolved in future. We apologize for any inconvenience.

@wknauf,

During further investigation, we have found that the problem you reported here is actually an expected behavior. In the past, we had implemented this behavior as per another issue WORDSNET-7912. Please see Aspose.Words for .NET 17.9 Release Notes for more details. You should explicitly set SaveOptions.UpdateLastSavedTimeProperty to get desired results.

So, there are two ways to update that field on save:

  1. Update document property before saving it

doc.BuiltInDocumentProperties.LastSavedTime = DateTime.Now;

or

2. Use SaveOptions with ‘UpdateLastSavedTimeProperty’ setted to true

doc.Save(MyDir + "20.1-result blank.docx", new PdfSaveOptions() { UpdateLastSavedTimeProperty = true });

Hope, this helps.

Thanks, this works.

But there is one problem: the date in the document is one hour in the future. I don’t know whether this is caused by our timezone (germany has GMT+1) or by daylight saving time. Current GMT when writing this post is 12:30, local time is 13:30, and the word document has 14:30.

Attached is a reworked sample which contains your suggestion.
SaveDateTest_2020-02-18.zip (4.7 MB)

Best regards

Wolfgang

@wknauf,

Thanks for the additional information. We will check this time offset issue on our end and keep you posted on any further updates.