0

I am trying to port C# Interop code for all Office apps (Outlook,Excel,Word,PowerPoint,etc) from NET Framework 4.8 to NET 5.0. But I am being blocked by the removal of Marshall.GetActiveObject from the NET 5 Marshal.* API.

Installing the package Microsoft.Windows.Compatibility did not help. Here is an example of code that used to work, but now fails for all apps.

var o = Marshal.GetActiveObject("Access.Application") as Access.Application;
var o = Marshal.GetActiveObject("Outlook.Application") as Outlook.Application;
var o = Marshal.GetActiveObject("Word.Application") as Word.Application;
var o = Marshal.GetActiveObject("Powerpoint.Application") as Excel.Application;

Can anyone please tell me how to access running instances of these apps under NET 5? I imagine that the Microsoft developers would not have removed the API without providing some (hopefully recommended) way of accessing running apps. Thank you

Kevin
  • 1,548
  • 2
  • 19
  • 34
  • Hi Charlie, YES that helped! It solved my problem totally. You made it look so easy; I marvel at how many times someone knows the precise answer and takes the time to help others. Thank you! (And on Christmas Day, no less. I never even knew that MS had posted such reference sources or that someone posted a workaround that you linked to. All of my searches (for a couple of hours) never found that link. My Christmas is better because of you. thanks again) – Kevin Dec 26 '21 at 02:47
  • Pleasure, it does seem hard to find similar posts sometimes. But I mean it's *literally* the third link down on Google for [Marshal.GetActiveObject](https://www.google.com/search?q=marshal.getactiveobject) so I don't understand why you couldn't find it. You should have an option above to accept this duplicate, which will close the question and display a banner with the link. – Charlieface Dec 26 '21 at 03:20
  • I see your point about the third link down when searching for Marshal.GetActiveObject. I never thought to search for that. I searched for various combinations of Interop, GetActiveObject, Office apps, and so on. And I searched around in the Microsoft doc, and here on SO. It's a puzzle to me that I didn't find the link you gave me. I'm trying to put your link in all my relevant questions for others. Thank you again. – Kevin Dec 26 '21 at 18:17

0 Answers0