vendredi 28 janvier 2022

How to find installed Excel language in C#

Currently I am using the below code to get the excel language

        string strExcelLang = string.Empty;            
        Microsoft.Office.Interop.Excel.Application tExcel = new Microsoft.Office.Interop.Excel.Application(); 
        CultureInfo cExcelCulture = new CultureInfo(tExcel.LanguageSettings.get_LanguageID(Microsoft.Office.Core.MsoAppLanguageID.msoLanguageIDInstall));
            strExcelLang = cExcelCulture.ToString();
        if (strExcelLang.ToUpper().Equals("EN-US") || strExcelLang.ToUpper().Equals("JA-JP"))
        {
            // do something;
        }

However sometimes in client machine we are getting the following error,

System.Runtime.InteropServices.COMException
  HResult=0x80080005
  Message=Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80080005 Server execution failed (Exception from HRESULT: 0x80080005 (CO_E_SERVER_EXEC_FAILURE)).
  Source=mscorlib
  StackTrace:
   at System.Runtime.Remoting.RemotingServices.AllocateUninitializedObject(RuntimeType objectType)
   at System.Runtime.Remoting.Activation.ActivationServices.CreateInstance(RuntimeType serverType)
   at System.Runtime.Remoting.Activation.ActivationServices.IsCurrentContextOK(RuntimeType serverType, Object[] props, Boolean bNewObj)
   at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
   at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
   at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
   at System.Activator.CreateInstance(Type type, Boolean nonPublic)
   at System.Activator.CreateInstance(Type type)

Using the excel interop is causing this error. But it won't occur in all machines. Since it is occurring in certain machines, I would like to change the approach to get the installed excel language.

What are the other ways we can achieve this?





Aucun commentaire:

Enregistrer un commentaire