tags:

views:

2456

answers:

3

Can I make C#.NET start outlook in the code?

In VB6 we use object 'Outlook.Application' and write:'

Set oOutlook = CreateObject("Outlook.Application") 
Set oNameSpace = oOutlook.GetNamespace("MAPI") 
Set oInbox = oNameSpace.Folders(1) 
 'Set oInbox = oInbox.Folders("Inbox")
oInbox.Display 
 'oOutlook.Quit 'Close All Outlook copies

Copy/Paste from link: http://www.ozgrid.com/forum/showthread.php?t=73886

+4  A: 

If you just want to start the outlook; using System.Diagnostics.Process would be the easiest way. :)

Chathuranga Chandrasekara
I think that will work :). Thanks!
jeje1983
Doesn't really apply to the code in the question, but ok
Chad Grant
+4  A: 

System.Diagnostics.Process will only start a process.

To do additional actions such choosing folders, you need to use Microsoft Visual Studio Tools for Office (VSTO). And here is it's reference. For example:

var outlook = new Microsoft.Office.Interop.Outlook.ApplicationClass();
outlook.Quit();
abatishchev
I am told that when using Office interop, never use the XXXClass classes. I think you mean var outlook = new Microsoft.Office.Interop.Outlook.Application();
foson
Thanks! Very interesting opinion. Do you know why - don't use? I have an application with complex Excel-to-Excel data processing where there a lot of Excel.ApplicationClass classes usings. Everything works absolutely correctly! Also http://msdn.microsoft.com/en-us/library/microsoft.office.interop.outlook.application.aspx is an interface, isn't it?
abatishchev
+2  A: 

You could use its ProgID to get the type and the activator

Type objectType = Type.GetTypeFromProgID("Outlook.Application");
object outlook = Activator.CreateInstance(objectType);

But using this in C# you will lose all type information (i.e. no IntelliSense) and you need to call some ugly method to invoke the operations with LateBinding (google for Type.Invoke)

Other option is add a reference to Microsoft.Office.Interop.Outlook.ApplicationClass, so you have compile time type information and create an instance for Outlook in the usual way

using Microsoft.Office.Interop.Outlook;
Microsoft.Office.Interop.Outlook.ApplicationClass outlook 
     = new Microsoft.Office.Interop.Outlook.ApplicationClass();

Or you could use my Late Binding Helper library and use it like this

Invoker outlook = BindingFactory.CreateAutomationBinding("Outlook.Application");
outlook.Method("Quit").Invoke();

No Intellisense with this one, but at least the library will save you from the ugly calls to Type.Invoke and give you a fluent interface instead.

Ricky AH