Microsoft.Office.Interop.Excel doesn't work on 64 bit
Asked Answered
P

1

8

I've encountered a problem when developing on MS Visual Web Developer 2008 Express Ed. Developing ASP.NET C# on Windows7 64 bit OS.

I'm trying to open an Excel document, but it gives me Old format or invalid type library. (Exception from HRESULT: 0x80028018 (TYPE_E_INVDATAREAD))

I did configured the build to all processors (Any CPU, x64, x86) but it doesn't work. I searched the answer on the internet but couldn't find out how to handle it.

The weird thing is the same code worked on the same system when I developed on Microsoft Visual C# 2010 Express! how come? isn't it the same dll working behind?

Do I need to change that COM dll so it'll work on x64 system?

Please help me, what can I do?

My code is:

using Excel = Microsoft.Office.Interop.Excel;
xlApp = new Excel.Application();
__Log("Openning " + excelFileName);
xlWorkBook = xlApp.Workbooks.Open(excelFileName, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(2);
Precursory answered 24/11, 2011 at 22:39 Comment(3)
I'm not completely sure this willl solve your problem, but if you have a 32-bit COM DLL, you probably need to use an x86 build of your project even if you are running on 64-bit Windows.Patsy
I think you've got the right idea @Andrew. If it's 32bit Excel, 64bit .net won't want to work with it. Build it as x86 and it should work.Stereotyped
Already tried that.. but the same happened, I dont understand why this problem is not occure in Visual C# and only in Web developerPrecursory
P
20

After digging the internet I found out that there is a bug in Microsoft Interop with COM objects (at least with my case which is MS Excel 2010).

The bug is that .NET checks that your thread (C# or VB code) localization is suitable to MS Excel localization you installed earlier, and if not it tells that the Microsoft.Office.Interop library is old or invalid.

Your thread localization is derived from your computer regional settings (from the control panel --> regional and language)

Then there are two options to solve this problem:

  1. To change your thread localization (by code)
  2. Install language pack for your Office

The first solution goes like this:

using System.Threading;     // For setting the Localization of the thread to fit
using System.Globalization; // the of the MS Excel localization, because of the MS bug

Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
excelFileName = System.IO.Path.Combine(excelPath, "Ziperty Buy Model for Web 11_11_2011.xlsm");

Hope it helps :) gr8 day

Precursory answered 25/11, 2011 at 22:49 Comment(2)
...for more info, the KB entry that corresponds: support.microsoft.com/default.aspx?scid=kb;en-us;320369Vulture
@Vulture : The KB seems to say this bug applies to VS 2005 and Excel 2007 (and older versions). The description seems to match but it might be an unrelated bug.Air

© 2022 - 2024 — McMap. All rights reserved.