none
why can method Excel.Application.WORKBOOKS.OPEN(FilePathName) work in English format, but can not work in Chinese format RRS feed

  • Question

  • I had created a little software in VS2008 that can open Excel file and do some process. This software worked fine in our Canadian office. But our colleague in Chinese office have problem with it. So I switched the region and language format in control panel of my computer from English(Canada) to Chinese(Simpliified, PRC), and debugged the program. Then I found the sentence with method "Excel.Application.WORKBOOKS.OPEN(FilePathName)" create the error.  Why can this method work in English format, but cannot work in Chinese format.

    My sample code is:

        Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click

            Dim xlApp As Excel.Application

            Dim xlWorkBook As Excel.Workbook

            Dim xlWorkSheet As Excel.Worksheet

            xlApp = New Excel.Application()

            xlApp.Visible = True

            'If IO.File.Exists("C:\Users\jluo\Documents\SolidworksTemplates\Macros\test.xlsx") Then xlWorkBook = xlApp.Workbooks.Open("C:\Users\jluo\Documents\SolidworksTemplates\Macros\test.xlsx")

            xlWorkBook = xlApp.Workbooks.Open("C:\Users\jluo\Documents\temp\test.xlsx") 'This one create an error when in Chinese format

            xlWorkSheet = xlWorkBook.Worksheets("Sheet1")

            xlWorkBook.Close(SaveChanges:=False)

            xlApp.Quit()

            releaseObject(xlApp)

            releaseObject(xlWorkBook)

            releaseObject(xlWorkSheet)

        End Sub

        Private Sub releaseObject(ByVal obj As Object)

            Try

                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)

            Catch ex As Exception

            Finally

                obj = Nothing

                GC.Collect()

            End Try

        End Sub

    The following is the detail of error:

    System.Runtime.InteropServices.COMException was unhandled

      ErrorCode=-2147319784

      Message="Old format or invalid type library. (Exception from HRESULT: 0x80028018 (TYPE_E_INVDATAREAD))"

      Source="Microsoft.Office.Interop.Excel"

      StackTrace:

           at Microsoft.Office.Interop.Excel.Workbooks.Open(String Filename, Object UpdateLinks, Object ReadOnly, Object Format, Object Password, Object WriteResPassword, Object IgnoreReadOnlyRecommended, Object Origin, Object Delimiter, Object Editable, Object Notify, Object Converter, Object AddToMru, Object Local, Object CorruptLoad)

           at WindowsApplication1.Form1.Button4_Click(Object sender, EventArgs e) in C:\Users\jluo\Documents\Visual Studio 2008\Projects\WindowsApplication1\WindowsApplication1\Form1.vb:line 105

           at System.Windows.Forms.Control.OnClick(EventArgs e)

           at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)

           at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)

           at System.Windows.Forms.Control.WndProc(Message& m)

           at System.Windows.Forms.ButtonBase.WndProc(Message& m)

           at System.Windows.Forms.Button.WndProc(Message& m)

           at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)

           at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

           at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)

           at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)

           at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)

           at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)

           at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()

           at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()

           at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)

           at WindowsApplication1.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81

           at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)

           at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()

           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)

           at System.Threading.ThreadHelper.ThreadStart()

      InnerException:

    Friday, September 7, 2012 8:52 PM

Answers

  • Hi Justin,

    Thank you for posting in the MSDN Forum.

    According to this thread, there are few workarounds for this problem. You can try below solutions:

      • Set thread's locale to "en-US" before calling into Excel.
      • Install Multilingual User Interface Pack.
      • Use VSTO's LcidProxy wrapper that will automatically use the 1033-based Invoke. Notice that you will have to install VSTO runtime.
         
        using Excel = Microsoft.Office.Interop.Excel;
        Excel.Application oApp = .... // somehow get the application obejct
        // wrap the application object into the LcidProxy wrapper
        oApp = (Excel.Application)Microsoft.Office.Tools.Excel.ExcelLocale1033Proxy.Wrap(typeof(Excel.Application), oApp);
        // now use it
        oApp.Workbook.Add .....

    For more inforamtion, you can refer to this knowledge base page. http://support.microsoft.com/kb/320369

    Hope it helps.

    Best regards,
    Quist


    Quist Zhang [MSFT]
    MSDN Community Support | Feedback to us

    Sunday, September 9, 2012 10:20 AM
    Moderator

All replies

  • Hi Justin,

    Thank you for posting in the MSDN Forum.

    According to this thread, there are few workarounds for this problem. You can try below solutions:

      • Set thread's locale to "en-US" before calling into Excel.
      • Install Multilingual User Interface Pack.
      • Use VSTO's LcidProxy wrapper that will automatically use the 1033-based Invoke. Notice that you will have to install VSTO runtime.
         
        using Excel = Microsoft.Office.Interop.Excel;
        Excel.Application oApp = .... // somehow get the application obejct
        // wrap the application object into the LcidProxy wrapper
        oApp = (Excel.Application)Microsoft.Office.Tools.Excel.ExcelLocale1033Proxy.Wrap(typeof(Excel.Application), oApp);
        // now use it
        oApp.Workbook.Add .....

    For more inforamtion, you can refer to this knowledge base page. http://support.microsoft.com/kb/320369

    Hope it helps.

    Best regards,
    Quist


    Quist Zhang [MSFT]
    MSDN Community Support | Feedback to us

    Sunday, September 9, 2012 10:20 AM
    Moderator
  • hi Quiet, thank you for  answer my question. I am in a business trip to Finland, and will return home after 1 week. After I return home, I will try your solution, and let you know if it works. 

    thanks again

    justin

    Monday, September 10, 2012 9:09 PM
  • Thank you very much!!!!

    I revised the software according to your suggestion. It works perfectly.

    Best regards,

    Justin

    Friday, September 21, 2012 5:38 PM