问题
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