How to resolve 'Unable to cast COM object' error with Microsoft.Office.Interop.Excel in Office 2021 with Access Database Engine installed?

Gaddam, Tejaswini 5 Reputation points
2023-11-03T14:22:23.2766667+00:00

I have a .net Application that uses Microsoft.Office.Interop.Excel nuget which is giving error in Office 2021 version.

Unable to cast COM object of type 'Microsoft.Office.Interop.Excel.ApplicationClass' to interface type 'Microsoft.Office.Interop.Excel._Application'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{000208D5-0000-0000-C000-000000000046}' failed due to the following error: Element not found. (Exception from HRESULT: 0x8002802B (TYPE_E_ELEMENTNOTFOUND)).

It was working fine with Office 2019 version but this issue is occurring with Office 2021 version.

I also tested that this issue is occurring only after installing access db engine 2016 x64 version: 16.0.5044.1000 (which is required component for the application to function properly).

I have tried uninstalling and reinstalling the Office 2021 but the issue still persists. Repairing the Office 2021 version solved the issue for one time. Next day it is causing same error. I have to do repair every time the issue occurs.

Why multiple repair is required? Is there any other approach to solve the issue apart from Office Repair?

Environment:

.NetFramework 4.7.2

Code to reproduce Issue:

using Microsoft.Office.Interop.Excel;

public class ExcelOperations
{
        private readonly _Application _excel;
        private Workbook _workbook;
      
        public ExcelOperations(string path)
		{
		   _excel = new Application();
		   _workbook = _excel.Workbooks.Open(path); // Error line COM Exception
		}
}
.NET
.NET
Microsoft Technologies based on the .NET software framework.
2,337 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
9,511 questions
Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
3,066 questions
{count} vote

1 answer

Sort by: Most helpful
  1. KOZ6.0 4,255 Reputation points
    2023-11-03T15:52:51.97+00:00

    Would you like to try using NetOfficeFw.Excel? You can get it from nuget.

    https://www.nuget.org/packages/NetOfficeFw.Excel

    Your code will look like this.

    //using Microsoft.Office.Interop.Excel;
    using NetOffice.ExcelApi;
    
    public class ExcelOperations
    {
        private _Application _excel;
        private Workbook _workbook;
    
        public void OpenWorkbook(string path) {
            _excel = new Application();
            _workbook = _excel.Workbooks.Open(path);
        }
    }