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?
Asked
Active
Viewed 5,110 times
4

Arlen Beiler
- 15,336
- 34
- 92
- 135
1 Answers
5
You can use System.Runtime.InteropServices.Marshal.GetActiveObject()
for this purpose.
(See this related question.)
-
This throws `Operation unavailable (Exception from HRESULT: 0x800401E3 (MK_E_UNAVAILABLE))` – Arlen Beiler Mar 05 '13 at 16:25
-
@ArlenBeiler: Is Outlook running? – cdhowie Mar 05 '13 at 16:28
-
8Yes. Actually, turns out one can't be running as administrator and the other normal, they both have to be the same. Thanks – Arlen Beiler Mar 05 '13 at 16:46
-
1@cdhowie can we add that last comment by Arlen to the answer? It just saved me endless agony and I almost overlooked it entirely! – Sandy Gifford Oct 17 '14 at 12:32
-
is there any simple easy way to check this condition, i.e., permissions match between client and server? – dr d b karron Mar 09 '19 at 00:22
-
@drdbkarron Yes, just catch the specific exception. – cdhowie Mar 09 '19 at 05:40
-
Exceptions are expensive operations I want to read permissions from both sides and check if the bitmasks are congruent ($them together). – dr d b karron Mar 09 '19 at 12:04
-
@drdbkarron Pretend that you proceed with the route of catching the exception. How many exceptions do you expect to be generating per second? – cdhowie Mar 12 '19 at 03:48
-
how did you solve it finally? – RRR May 16 '19 at 11:22