1

I've been unable to get the Microsoft.Office.Interop.Outlook library to work the way I need it to in .NET Core, but it works fine in .NET Framework. As a result I want to build my main application as a .NET Core project, and only call the .NET Framework part as needed (from another project within the same solution).

So I've got my .Net Framework project, let's call it TESTOutlookInterop. It's got one class that looks like this:

using Microsoft.Office.Interop.Outlook;

namespace TESTOutlookInterop
{
    public class OutlookInterop
    {
        public string GetSelectedMessage()
        {
            Application outlook = new Application();
            OlSelectionLocation select = outlook.ActiveExplorer().Selection.Location;
            string selection = outlook.ActiveExplorer().Selection[1].EntryID;
            return selection;
        }
    }
}

If I set the dropdown menu on top and set it to TESTOutlookInterop and hit 'Start' this works fine (when I call GetSelectedMessage() from TESTOutlookInterop's Main method it returns the string I'm looking for, in other words). For some context, this basically looks at the current Outlook process and returns the ID of whatever message is currently highlighted.

Now I've got a second project in this solution called TESTcore. I try to call the GetSelectedMessage() method like this:

using TESTOutlookInterop;
namespace TESTCore
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            OutlookInterop outlookinterop = new OutlookInterop();           
            ResultLabel.Content = outlookinterop.GetSelectedMessage();
        }
    }
}

If I set the dropdown in VS to run 'TESTcore' and run the Core app the 'GetSelectedMessage()' method gets called it behaves the same way it does when I try to run it in .NET Core (doesn't work at all, seemingly because the interop library is not compatible with Core). So how I do I call this interop method in the .NET Framework project from the Core project correctly?

proudfeet
  • 43
  • 2
  • 3
    That's not possible. Get ahead by targeting .NET 5, currently at RC and slated for release in November. It brings support back for the dynamic binder. – Hans Passant Sep 18 '20 at 21:02
  • You could start the .net process and pass json messages via stdin / stdout... I once used a similar approach to proxy from a 64bit .net process to interop with 32bit office. – Jeremy Lakeman Sep 21 '20 at 07:11
  • The duplicates show that yes, it's possible, as long as you use `Add COM Reference`. An answer probably posted by Hans Passant long ago explains that Office Interop libraries were essentially workarounds for the incomplete COM support in older .NET Old versions. In .NET 1.x, there were no optional parameters, no `dynamic` type, so the COM proxies generated by Visual Studio were... a bit hard to use. You should be able to use any Office application directly by using `Add COM Reference` – Panagiotis Kanavos Sep 21 '20 at 07:16

1 Answers1

0

You can try the following code to use outlook interop in the .net core app.

First, please create a .net core app(I used console app).

Second, right click Dependencies-> choose Add COM Reference.

Third, please add Microsoft Outlook 16.0 Object Library in the tab.

enter image description here

Fourth, please set "Embed Interop Types" to "Yes".

Finally, you can use the related code in the .net core app.

using Microsoft.Office.Interop.Outlook;

 static void Main(string[] args)
        {
            Application outlook = new Application();
            MailItem selection = outlook.ActiveExplorer().Selection[1];
            Console.WriteLine(selection.EntryID);
            Console.WriteLine(selection.Subject);
        }

enter image description here

Jack J Jun
  • 5,633
  • 1
  • 9
  • 27
  • Please try my solution first. I used .net core 3.1. It comes from [Outlook Interop in .NET Core 3.0?](https://stackoverflow.com/questions/56442139/outlook-interop-in-net-core-3-0) – Jack J Jun Sep 21 '20 at 07:13
  • In that case you should have closed this as a duplicate, not post your own answer *without* attribution, forgeting to mention the *most important* part - that the library should be added as a COM Reference. SO has very different rules from Technet Forums. An answer has to be complete, not just a set of steps without explanation. And it's definitely not a good idea to answer when a duplicate already exists. It increases noise and makes it *harder* for people to solve their problems – Panagiotis Kanavos Sep 21 '20 at 07:16