Convert SaveDate Field to Static Text Issue

Hi,

I have a problem with the attached document (Test Convert SaveDate.zip) (21.1 KB). I have tried to simplify the issue as much as possible.

There are 2 dates in the document Created (24/08/2017 15:46) and Last Modified (05/09/2017 10:16). In the footer there is a field:

{ SAVEDATE \@ "d.M.yyyy" \* MERGEFORMAT }

which correctly shows “5.9.2017”. You can update the field value and it doesn’t change so it’s reading it fine.

When using the sample code (Programming-Documents/Fields/ConvertFieldsInDocument.cs) and changing the Fields Helper to convert Save Date as such:

FieldsHelper.ConvertFieldsToStaticText(doc, FieldType.FieldSaveDate);

The Date thats written is the Creation Date not the Saved Date. Ie the resuling test that’s added is “24.8.2017” whcih is wrong.

Can you see any reason why this happens and how to fix it?

Regards,

@GaryO,

Thanks for your inquiry. Please call the Document.UpdateFields method before FieldsHelper.ConvertFieldsToStaticText as shown below to get the desired output.

Document doc = new Document(MyDir + "Test Convert SaveDate.docx");
doc.UpdateFields();
FieldsHelper.ConvertFieldsToStaticText(doc, FieldType.FieldSaveDate);
                
doc.Save(MyDir + "17.9.docx");

Thank you Tahir,

For future reference, is there any reason it was picking up the Created Date? It’s a little worrying that the helper picks the wrong date without updating.

Hello again Tahir,

After some testing, it appears your solution is not going to work for us. In our process we copy the document into a temp folder for processing. Running UpdateFields() would also update:

{ FILENAME \p \* MERGEFORMAT }

which is not acceptable for us as it must keep that printed value. Any help in understanding why it would use the Created Date would be approciated so we know how to handle the situation in the future.

I have placed an updated document for you to test here: Test Convert SaveDate.zip (21.3 KB)

If you notice the current value of the file path is “C:\Program Files (x86)\Aspose\Aspose.Words for .NET\Examples\CSharp\Programming-Documents\Fields\Data\Test Convert SaveDate - Copy.docx”

If we run ConvertFieldsToStaticText it will keep that vlaue, if we update the fields though, it will pick the new filename and file path (not acceptable for what we need). we’d like the FieldSaved date method to work the same, ie to use the value as shown on the document. Again, any insight into understanding why the Creation date is used will help us. Whether or not it uses a preupdate or post update value, it should never use the Creation date I would have thought.

@GaryO,

Thanks for your inquiry. A field in a Word document is a complex structure consisting of multiple nodes that include field start, field code, field separator, field result and field end. The content between the field start and separator is the field code. The content between the field separator and field end is the field result. Please refer to the following article.
Working with Fields

In your document, the value (field result) of SAVEDATE FIELD is 24.8.2017. This is the reason you are getting this value without updating the field.

In your case, we suggest you please update only SAVEDATE field as shown below. Please use Field.Update method to update only field and Field.Unlink method to unlink field. Hope this helps you.

Document doc = new Document(MyDir + "Test Convert SaveDate.docx");
//Update only SaveDate field.
doc.Range.Fields.Cast<Field>().Where(f => f.Type == FieldType.FieldSaveDate).ToList().ForEach(f => f.Update());
//Unlink only SaveDate field.
doc.Range.Fields.Cast<Field>().Where(f => f.Type == FieldType.FieldSaveDate).ToList().ForEach(f => f.Unlink());

doc.Save(MyDir + "17.9.docx");

Thanks Tahir.

For the moment I think we’ll Convert the FileName before running an update in case other field types show the same issue.

I’m still confused why the field result is 24.8.2017. When I open the document, it’s always showing 5.9.2017. I don’t have any of the “Update Fields” settings on in Word and I thought they only come in play when you go to print.

With the .Unlink() function, does it have any difference internally to the FieldsHelper? Which is better to use?

@GaryO,

Thanks for your inquiry. Please note that Aspose.Words mimics the same behavior as MS Word does. The value “24.8.2017” is stored in your document. Please unzip your document and check footer1.xml and footer2.xml.

MS Word updates this value from built-in document property i.e. last modified. You can get this value using Document.BuiltInDocumentProperties.LastSavedTime property.

Yes, you can use Field.Unlink method instead of FieldsHelper utility.