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
Best Way to Handle Integer Overflow in C#
Static Property Using Inotifypropertychanged. C#
Convert Transparent Png to Jpg with Non-Black Background Color
Why Is There Not a 'Fieldof' or 'Methodof' Operator in C#
Simple Delegate (Delegate) VS. Multicast Delegates
How to Run Something in the Sta Thread
Is There a "Between" Function in C#
Build Visual Studio Project Through the Command Line
ASP.NET MVC: Calling a Method from a View
Multi Processes Read&Write One File
Binding Datagrid to Observablecollection<Dictionary>
Xml Parsing - Read a Simple Xml File and Retrieve Values
How to Find the Fully Qualified Name of an Assembly
How to Fill Forms and Submit with Webclient in C#
How to Tell If My Application Is Running as a 32-Bit or 64-Bit Application