VSTO Excel C# Databinding

SSinhg 286 Reputation points
2021-04-01T11:05:22.58+00:00

Hi,

I'm using VS - 2019 and VSTO for my 2010 office excel add-in development

I'm implementing section 3 from the reference

I'm want to populate data on to excel on button click...
- I have populated data on dataTable
- But can't get pass code below error code...

Code

using DataTable = System.Data.DataTable;  
using Worksheet = Microsoft.Office.Interop.Excel.Worksheet;  
  
  
Worksheet worksheet = (Worksheet)Globals.Factory.GetVstoObject(  
                    Globals.ThisAddIn.Application.ActiveWorkbook.ActiveSheet  
                    );  

Error:

Unable to cast COM object of type 'System.__ComObject' 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: Library not registered. (Exception from HRESULT: 0x8002801D (TYPE_E_LIBNOTREGISTERED)).  

Appreciate any help!

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.
10,277 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,508 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Daniel Zhang-MSFT 9,616 Reputation points
    2021-04-02T02:08:50.257+00:00

    Hi SwatantraSingh-2674,
    First, the type of the extended object has the same name as the type of the native Office object, but the type is defined in the Microsoft.Office.Tools.Excel or Microsoft.Office.Tools.Word namespace.
    To generate a host item for an Excel worksheet, you can refer to the following code:

    Microsoft.Office.Interop.Excel.Worksheet nativeWorksheet =Globals.ThisAddIn.Application.ActiveSheet;  
    if (nativeWorksheet != null)  
    {  
        Microsoft.Office.Tools.Excel.Worksheet vstoSheet =   
            Globals.Factory.GetVstoObject(nativeWorksheet);  
    }  
    

    More details you can refer to this document.
    And the error may say it can't load a DLL.
    Generally, a simple Office repair can be performed from the "Add\Remove" program to solve the problem.
    If not, please try to delete invalid registry entries left by Office.
    Here are some related threads:
    Unable to cast COM object of type 'Microsoft.Office.Interop.Excel.ApplicationClass' to interface type 'Microsoft.Office.Interop.Excel._Application'
    How to solve “Unable to cast COM object of type Microsoft.Office.Interop.Excel.ApplicationClass’ to interface type ‘Microsoft.Office.Interop.Excel._Application’”
    Best Regards,
    Daniel Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments