The Excel Locale Issue

When you create Excel solutions with VBA, the Excel object model expects English US formatting regardless of the user's locale settings. VSTO mimics this behavior by creating transparent proxies for the objects that always report that the locale is English US (locale ID 1033). Although the Excel object model expects locale ID 1033, it formats the data according to the user's locale settings when the data is displayed in the worksheet.

The AssemblyInfo.vb file within your Excel solutions contains an attribute called ExcelLocale1033Attribute. The value of this attribute determines whether VSTO creates and uses the transparent proxies and whether or not it behaves in the same way as VBA. By default, this attribute is set to true, causing the Excel object model to always expect locale ID 1033 (the same behavior you get in VBA). If you change this attribute to false, VSTO does not automatically report the locale as English US; instead, the current culture is reported. This can cause errors or unexpected behavior if an English edition of Excel is installed without a Multilingual User Interface (MUI) pack. You typically do not need to change the default setting.

If you must change the default behavior of VSTO by setting the ExcelLocale1033Attribute to false, you must take additional steps to ensure that your code will behave as expected if run on computers with different Windows regional settings. You should also avoid using string literals when you pass parameters or assign values to Excel objects. For example, instead of using a string such as "6/10/07," you should use a System.DateTime object that you can pass to Excel. This is because the String might be interpreted differently in different locales. In one locale, it might be interpreted as June 10, 2007 but interpreted as October 6, 2007 in another locale. If you don't change the default setting for ExcelLocale1033Attribute, there might be some cases where you might want to "unwrap" a particular proxy to access the native PIA object. You can do this by using the Microsoft.Office.Tools.Excel.ExcelLocale1033.Unwrap method of the ExcelLocale1033Proxy class. Be sure to restore the object to its proxied state by calling the Wrap method.

Was this article helpful?

+1 0

Post a comment