none
Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.VisualStudio.Tools.Office. in vsto RRS feed

  • Question

  • Hi 

    I am trying to cast the Com object to Microsoft.Office.Tools.Excel.Worksheet.

    it is throwing the below error:

    System.InvalidCastException: 'Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.VisualStudio.Tools.Office.Runtime.Interop.IHostItemFactoryNoMAF'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{A0885C0A-33F2-4890-8F29-25C8DE7808F1}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).'

    Here is where I am getting the error

                          Microsoft.Office.Tools.Excel.Worksheet ExtendedWorkSheet = Globals.Factory.GetVstoObject(ActiveWorkSheet); // In this line of code I am getting the error.

    Here is my code

    private async void btnData_Click(object Sender, RibbonControlEventArgs RibbonEvents)
    {

        // to change to Async call

        Tuple<DataSet, string> TuResults = await obj.GetMasterDataForUpdate(TableName, Year);

        DataSet DsMasterData = TuResults.Item1;


        if (DsMasterData != null)
        {
            Worksheet ActiveWorkSheet = CreateNewWorkSheet(SHEET_NAME);
            Microsoft.Office.Tools.Excel.Worksheet ExtendedWorkSheet = Globals.Factory.GetVstoObject(ActiveWorkSheet);

        }

    }

    Any help on how to resolve the issue will be helpful.

    Thanks

    Nihar

    Tuesday, August 8, 2017 10:14 AM

All replies

  • Hi NiharSai,

    It seems you want to convert a Microsoft.Office.Interop.Excel.WorkSheet object to a Microsoft.Office.Tools.Excel.Worksheet, right? I failed to reproduce your issue and I think the code is OK.

    According to my search result, there may be wrong with the Microsoft.VisualStudio.Tools.Office.Runtime.Interop.IHostItemFactoryNoMAF assembly, I suggest you try to re-install VSTO Runtime to repair the issue.

    By the way, what's your current issue in this thread?

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/880f5d44-59d0-478e-860a-42686b928ac4/how-to-select-excel-cell-based-on-font-color-in-the-given-range?forum=vsto#9d08c464-e825-4772-bf9e-41edf057b143

    If it has been solved, I suggest you mark a helpful answer or provide your solution to close that thread. If not, please feel free to let us know your current issue.

    Best Regards,

    Terry

    Wednesday, August 9, 2017 7:56 AM
  • Hi NiharSai,

    Has your issue been solved now? What's your current issue?

    Is ActiveWorkSheet an Interop WorkSheet object?

    Could you try to test below code to check if it could work for you?

                Excel.Worksheet ActiveWorkSheet = Globals.ThisAddIn.Application.ActiveSheet;
                Microsoft.Office.Tools.Excel.Worksheet  ExtendedWorkSheet = Globals.Factory.GetVstoObject(ActiveWorkSheet);

    Besides, I suggest you show us the whole code in CreateNewWorkSheet function so we could try to reproduce your issue.

    Best Regards,

    Terry


    Thursday, August 24, 2017 9:34 AM
  • Did you ever get a resolution to this issue?  I am having similar issue, and it confounds me. This is something where a form created within an Excel interop add-on that calls a task from custom class object of mine.  It's all contained, and none of the excel add-in class objects return anything related to an async task.  I'm wondering if somewhere this is exposing a symptom in my code where perhaps I am mixing and matching _worksheet interface with a worksheet class.
    Monday, July 6, 2020 1:43 PM