No definition found for GetActiveObject from System.Runtime.InteropServices.Marshal C#

和自甴很熟 提交于 2020-04-30 10:48:36

问题


I'm trying to connect to a running Excel instance, but when I try to use the following code snippet:

using Microsoft.Office.Interop.Excel;
using System.Runtime.InteropServices;

public Application StartExcel()
{
    Application instance = null;
    try
    {
        instance = (Application)Marshal.GetActiveObject("Excel.Application");
    }
    catch (COMException ex)
    {
        instance = new ApplicationClass();
    }

    return instance;
}

I found similar snippets on the internet, but when I compile this code I get the following error:

error CS0117: 'Marshal' does not contain a definition for 'GetActiveObject'.

I do not know why, because I have the following NuGet packages: - Microsoft.Office.Interop.Excel - System.Runtime.InteropServices


回答1:


This is available only .Net Framework but not for .Net Core . Please check the .Net project type .

https://docs.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.marshal.getactiveobject?view=netframework-4.8&viewFallbackFrom=netcore-2.2



来源:https://stackoverflow.com/questions/58010510/no-definition-found-for-getactiveobject-from-system-runtime-interopservices-mars

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!