Search code examples
c#excelvisual-studiocomoffice-interop

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


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?


Solution

  • 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