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

Free Support Forum - aspose.com

UpdateFields() not doing as Word with date formatting in DOCVARIABLE fields

Example:

Create a document and add a document variable named "A" with value "2013-05".

In the document add the field

A (no formatting): { DOCVARIABLE A }

then

A (with formatting): { DOCVARIABLE A @ "MMMM yyyy" }

If one open document in Word and update the fields one will get formatting according to the LanguageID of the range where the field is.

So setting it to wdSwedish would give "maj 2013".

Setting it to wdNorwegianBokmol would give "mai 2013".

Setting it to wdEnglishUS would give "May 2013".

Using Aspose.Words.UpdateFields() it appears that Range.LanguageID is not used but rather the settings in (on Windows 7) Region and Language > Formats > Format.

Am I correct?

Stein-Tore Erdal

Hi Stein,


Thanks for your inquiry. In case you are using an older version of Aspose.Words, I would suggest you please upgrade to the latest version (v13.4.0) from here and let us know how it goes on your side.

Aspose.Words support the requested feature. Following code example adds a document variable and update the variable according to the DOCVARIABLE formatting. I have attached the input and output document with this post for your kind reference.

// Store the current culture so it can
be set back once mail merge is complete.
<o:p></o:p>

CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;

Thread.CurrentThread.CurrentCulture = new CultureInfo("sv-SE");

Document doc = new Document(MyDir + "DocVariable.docx");

doc.Variables.Add("CURRENT_DATE", "2013-05");

DocumentBuilder builder = new DocumentBuilder(doc);

doc.UpdateFields();

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

// Restore the original culture.

Thread.CurrentThread.CurrentCulture = currentCulture;


Hi Tahir,

What you show is as I experienced. (I have version 13.2.0.0 of Aspose.Words.dll.)

Here is my code for the same:

string outFilePath  = @“c:\tmp\out.docx”;
System.Globalization.CultureInfo currentCulture = System.Threading.Thread.CurrentThread.CurrentCulture;
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(“sv-SE”);
Aspose.Words.Document doc = new Aspose.Words.Document();
doc.Variables.Add(“CURRENT_DATE”, “2013-05”);
Aspose.Words.DocumentBuilder builder = new Aspose.Words.DocumentBuilder(doc);
builder.InsertField(“DOCVARIABLE CURRENT_DATE \@ “MMMM yyyy””);
doc.UpdateFields();
doc.Save(outFilePath);
System.Threading.Thread.CurrentThread.CurrentCulture = currentCulture;
System.Diagnostics.Process.Start(outFilePath);

Now you should see “maj 2013” in the document opened by MS Word.
(I use Word 2010 with SP1 MSO and english menuitems)
When I select the field I can see “English (U.S.)” in Language field in the status bar.
When I right-click on the field and select “Update Field” the value of the DOCVARIABLE field is changed to “May 2013”.

So it seems Aspose use another logic (looking at CultureInfo) for applying date formatting to fields that MS Word does.
If this is the case it is a bit unfortunate as one may get different results if a user open the document in MS Word.
In our case we have a Word template which is used with Aspose to fill in some information and then save as pdf. So here we get formatting of our date field according the CurrentCulture on the computer the process is being run on.
We also use the same template to generate a Word document which is then given to the user for further editing in MS Word. Now when the user print the document the date field is updated by MS Word and get formatting according to the LanguageID for the given range (regardless of CurrentCulture as far I can see).

Stein-Tore

Hi Stein,


Thanks for your inquiry. Please use the Font.LocaleId property to get or set the locale identifier (language) of the formatted characters. Please use the following code snippet to achieve your requirements and let us know if you have any more queries.

// Store the current culture so it can
be set back once mail merge is complete.
<o:p></o:p>

CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;

Thread.CurrentThread.CurrentCulture = new CultureInfo("sv-SE");

Document doc = new Document();

doc.Variables.Add("CURRENT_DATE", "2013-05");

DocumentBuilder builder = new DocumentBuilder(doc);

// For the list of locale identifiers see http://www.microsoft.com/globaldev/reference/lcid-all.mspx

builder.Font.LocaleId = 1053;

builder.InsertField("DOCVARIABLE CURRENT_DATE \\@ \"MMMM yyyy\"");

doc.UpdateFields();

doc.UpdateFields();

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

// Restore the original culture.

Thread.CurrentThread.CurrentCulture = currentCulture;


Hi Thair,

A bit difficult to explain.

The issue is that Word does it different from Aspose when comes to applying date formatting when updating fields.

In the example below I try to show it.
Here I wish to show year and month twice in the same document, first in swedish format, then in norwegian format.
Using Aspose I don't see how it is possible.
Only after using an instance of Word to update the fields will I get the correct date format according to LocalId for the text where the fields are.

Aspose.Words.Document doc = new Aspose.Words.Document();
doc.Variables.Add("CURRENT_DATE", "2013-05");
Aspose.Words.DocumentBuilder builder = new Aspose.Words.DocumentBuilder(doc);

// Swedish
builder.Font.LocaleId = 1053;
builder.Write("Year and month in swedish: ");
builder.InsertField(“DOCVARIABLE CURRENT_DATE \@ “MMMM yyyy””);
builder.Writeln();
// Norwegian
builder.Font.LocaleId = 1044;
builder.Write("Year and month in norwegian: ");
builder.InsertField(“DOCVARIABLE CURRENT_DATE \@ “MMMM yyyy””);

// Set culture to swedish before calling UpdateFields()
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(“sv-SE”);
doc.UpdateFields(); // Note, Aspose looks at CurrentCulture instead of LocaleId when applying date formatting.

// Save the file and open it in Word
string outFilePath = @“c:\tmp\out” + DateTime.Now.ToString(“HHmmssffff”) + “.docx”;
doc.Save(outFilePath);
System.Diagnostics.Process.Start(outFilePath);
// Now you should see “maj 2013” in both fields. I would expect it to show “mai 2013” in the last field.
// Try to right-click on the last field (text here having norwegian language id).
// Now you should see it change to “mai 2013”. This is what I expect.

Hi Stein,


Thanks for your inquiry. I have managed to reproduce the same issue at my side. I have logged this issue as WORDSNET-8330 in our issue tracking system. I have linked this forum thread to the same issue and you will be notified via this forum thread once this issue is resolved.

We apologize for your inconvenience.

Hi Stein,


It is to inform you that our development team has completed the work on the issue (WORDSNET-8330) and has come to a conclusion that this issue and the undesired behavior you’re observing is actually not a bug in Aspose.Words. So, we have closed this issue as ‘Not a Bug’.

Please set Document.FieldOptions.FieldUpdateCultureSource to FieldUpdateCultureSource.FieldCode before updating the fields to get the desired behavior as shown in following code snippet.


// Open document<o:p></o:p>

Aspose.Words.Document doc = new Aspose.Words.Document();

doc.Variables.Add("CURRENT_DATE", "2013-05");

Aspose.Words.DocumentBuilder builder = new Aspose.Words.DocumentBuilder(doc);

//System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("sv-SE");

builder.Font.ClearFormatting();

// Swedish

builder.Font.LocaleId = 1053;

builder.Write("Year and month in swedish: ");

Field field1 = builder.InsertField("DOCVARIABLE CURRENT_DATE \\@ \"MMMM yyyy\"");

builder.Writeln();

//field1.Update();

//System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("zh-HK");

// Norwegian

builder.Font.LocaleId = 1044;

builder.Write("Year and month in norwegian: ");

Field field2 = builder.InsertField("DOCVARIABLE CURRENT_DATE \\@ \"MMMM yyyy\"");

builder.Writeln();

//field2.Update();

//doc.FieldOptions.FieldUpdateCultureSource = FieldUpdateCultureSource.CurrentThread;

// Set culture to swedish before calling UpdateFields()

System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("sv-SE");

doc.FieldOptions.FieldUpdateCultureSource = FieldUpdateCultureSource.FieldCode;

doc.UpdateFields(); // Note, Aspose looks at CurrentCulture instead of LocaleId when applying date formatting.

// Save the file and open it in Word

string outFilePath = @"c:\tmp\out" + DateTime.Now.ToString("HHmmssffff") + ".docx";

doc.Save(outFilePath);

System.Diagnostics.Process.Start(outFilePath);


The issues you have found earlier (filed as ) have been fixed in this update. This message was posted using BugNotificationTool from Downloads module by MuzammilKhan