No Definition Found for Getactiveobject from System.Runtime.Interopservices.Marshal C#

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

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

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

Is there a substitue for System.Runtime.InteropServices.Marshal.GetActiveObject() in .NET Core?

That specific API is gone, but you can easily P/Invoke GetActiveObject to get that information.

GetActiveObject is not a member of Marshal

The answer was posted by @Simon_Mourier in the comments on the original post. The Visual Studio project was targeting .NET Core. To fix the issue I simply switched the project to target .NET Framework and the error went away.

com exception excel InteropServices.COMException - c#

I think your issue is Visual Studio running in a different context to Excel.

I tried a slightly modified version of your code and it works just fine when running Visual Studio NOT as admin (same user as excel was opened with).

Might be worth checking you are using the right version of the interop dll also.

public bool IsExcelOpened()
{
bool isOpened = false;

try
{
Excel.Application exApp;
Excel.Worksheet xlWorksheet;
exApp = System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application") as Excel.Application;
if(exApp != null)
{
xlWorksheet = (Excel.Worksheet)exApp.ActiveSheet;
isOpened = true;
}
}
catch { }
return isOpened;
}

How do I get the current excel filename in c#

Try this example:

using System;
using Excel = Microsoft.Office.Interop.Excel;

namespace Foo
{
class Program
{
static void Main(string[] args)
{
var excelApplication = (Excel.Application)CustomMarshal.GetActiveObject("Excel.Application");
var currentWorkbook = excelApplication.ActiveWorkbook;
Console.WriteLine("Current active Excel Workbook: " + currentWorkbook.Name); // or FullName to get full path to opened file
Console.ReadKey();
}
}
}

CustomMarshal and GetActiveObject() explanations can be found here:
No definition found for GetActiveObject from System.Runtime.InteropServices.Marshal C#



Related Topics



Leave a reply



Submit