How to connect to a running instance of outlook fr

2019-05-11 03:23发布

问题:

This question already has an answer here:

  • Getting/Creating an Outlook Application in Windows 7 5 answers

In VBA, I usually use appOutlook = GetObject(, "Outlook.Application") to get a running instance of Outlook, and it throws an error if Outlook isn't running. I normally did this in excel, but now I am wanting to see if I can get better results by moving to C# and doing it from there. What I need to do is copy emails from outlook and paste them into excel so that the tables in the email get pasted into the spreadsheet as cell values rather than html. However, I'm not quite sure how to do this. I usually use Activator.CreateInstance, but it makes a new instance. How can I connect to the current instance of Excel and Outlook?

回答1:

You can use System.Runtime.InteropServices.Marshal.GetActiveObject() for this purpose.

(See this related question.)