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:
References I have tried
All three of these references give me the same exception. Any ideas how to load this?
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.
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.
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.
User contributions licensed under CC BY-SA 3.0