Error loading Office.dll COMException: 'Errorloading type library/DLL'

0

I have a working Excel COM library and I am trying to use the following method to determine if excel is in edit mode

public bool IsEditMode(Microsoft.Office.Interop.Excel.Application xlApp)
    {
        //https://stackoverflow.com/questions/464196/workaround-to-see-if-excel-is-in-cell-edit-mode-in-net
        //xlApp = (Microsoft.Office.Interop.Excel.Application)ExcelDnaUtil.Application;
        var bars = xlApp.Application.CommandBars;
        var commandBar = bars["Worksheet Menu Bar"];
        var menu = commandBar.FindControl(
                   1, //the type of item to look for
                   18, //the item to look for
                   Type.Missing, //the tag property (in this case missing)
                   Type.Missing, //the visible property (in this case missing)
                   true);
        if (menu != null)
        {
            // Check if "New" menu item is enabled or not.
            if (!menu.Enabled)
            {
                return true;
            }
        }
        return false;
    }

When my code hits xlApp.Application.CommandBars; I get the following exception.

System.Runtime.InteropServices.COMException: 'Error loading type library/DLL. (Exception from HRESULT: 0x80029C4A (TYPE_E_CANTLOADLIBRARY))'

I believe the issue is because I am referencing the wrong version of the office.dll. Either it is targeting the wrong version of office or the wrong version of Visual Studio.

My version numbers:

  • VS 2017 Community (15.8.6)
  • Excel 2010 Office Standard 32-bit (14.0.7214.5000)

References I have tried

  • Manually add reference to C:\Windows\assembly\GAC_MSIL\office\14.0.0.0__71e9bce111e9429c\OFFICE.DLL
  • Let visual studio add the reference automatically to C:\Program Files (x86)\Microsoft Visual Studio\Shared\Visual Studio Tools for Office\PIA\Office14\office.dll
  • VS Reference Manager -> COM -> Microsoft Office 14.0 Object Library. (C:\WINDOWS\assembly\GAC_MSIL\Office\15.0.0.0__71e9bce111e9429c\Office.dll)

All three of these references give me the same exception. Any ideas how to load this?

c#
excel
visual-studio
com
office-interop
asked on Stack Overflow Oct 29, 2018 by Jon Kump

3 Answers

2

The issue was with a typelib registration from a different version of Office. I ended up deleting registry entries for all versions of Office that were no longer installed.

These links gave me the info I needed to get it working.

https://social.msdn.microsoft.com/Forums/vstudio/en-US/ac50fa41-8d47-4fa9-81a3-914f262676af/0x80029c4a-typeecantloadlibrary?forum=vsto

http://kb.palisade.com/index.php?pg=kb.page&id=528

answered on Stack Overflow Oct 29, 2018 by Jon Kump
1

I encountered this error since I upgraded from a 32 bit to 64 bit version of Office, which somehow resulted in Win32 and Win64 entries existing simultaneously in registry.

The solution that worked for me was deleting Win32 entry from registry in

Computer\HKEY_CLASSES_ROOT\TypeLib\{00020813-0000-0000-C000-000000000046}\1.9\0

since the Data was referring to invalid path

C:\Program Files (x86)\Microsoft Office\Root\Office16\EXCEL.EXE

Note: Do keep a backup of the entry via Export just in case something goes wrong.

answered on Stack Overflow Dec 6, 2019 by ScriptKraft
0

Have you tried changing "Microsoft.Office.Interop.Excel.Application" to dynamic? This should get around your issue with the library version. However you will lose intellisense for the object. Have a go just as a process of elimination firstly.

answered on Stack Overflow Oct 29, 2018 by Dev

User contributions licensed under CC BY-SA 3.0