1
votes

I am trying to write some c# code to interact with Outlook 2010. I am currently using this example from Microsoft.

My code follows:

using System;
using System.Text;          // StringBuilder
using System.Diagnostics;   // Debug

using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;

using Outlook = Microsoft.Office.Interop.Outlook;

namespace DirectReports
{
    public class Program
    {
        private void GetManagerDirectReports()
        {
            Outlook.AddressEntry currentUser = Application.Session.CurrentUser.AddressEntry;
            //Outlook.AddressEntry currentUser = Outlook.Application.Session.CurrentUser.AddressEntry;
            if (currentUser.Type == "EX")
            {
                Outlook.ExchangeUser manager = currentUser.GetExchangeUser().GetExchangeUserManager();
                if (manager != null)
                {
                    Outlook.AddressEntries addrEntries =
                        manager.GetDirectReports();
                    if (addrEntries != null)
                    {
                        foreach (Outlook.AddressEntry addrEntry
                            in addrEntries)
                        {
                            Outlook.ExchangeUser exchUser = addrEntry.GetExchangeUser();
                            StringBuilder sb = new StringBuilder();
                            sb.AppendLine("Name: " + exchUser.Name);
                            sb.AppendLine("Title: " + exchUser.JobTitle);
                            sb.AppendLine("Department: " + exchUser.Department);
                            sb.AppendLine("Location: " + exchUser.OfficeLocation);
                            Debug.WriteLine(sb.ToString());
                        }
                    }
                }
            }
        }
    }
}

The Microsoft example mentions "If you use Visual Studio to test this code example, you must first add a reference to the Microsoft Outlook 15.0 Object Library component". I am working in Visual Studio Express 2013 for Windows Desktop. I did not see a version 15.0 object library, but added the version 14.0 one instead (which I think it the right one for Outlook 2010 anyways):

references included

When I attempt a build, I get the following error:

The name 'Application' does not exist in the current context

I read a couple of references that indicate Application should be part of the above object libraries, but obviously it is not working here. Can someone please suggest what I am doing wrong?

3

3 Answers

6
votes

You can create a new Application object:

var appOutlook = new Microsoft.Office.Interop.Outlook.Application();

And then use it as:

Outlook.AddressEntry currentUser = appOutlook.Session.CurrentUser.AddressEntry;
1
votes

You are using the wrong project. When you create a new project in Visual studio, use The Outlook Add-in template. (Templates -> Visual C# -> Office -> Outlook).

In this code they Application.Session wil work like you expect.

Or you should create a new application object like this. var outlook = new Microsoft.Office.Interop.Outlook.Application(); And use outlook.Session.

-1
votes

Add the following line at the beginning of the file:

 using Microsoft.Office.Interop.Outlook;

Or just prepend any Outlook object declaration with the Outlook alias.

You may find the C# app automates Outlook (CSAutomateOutlook) sample project helpful.