none
Troubles Extending Outlook to a C# Windows Form Application RRS feed

  • Question

  • I'm sure it's something simple and I'm just a dope, but I'm having trouble with an app I'm tring to write.  When I make this code an "Outlook 2010 Add-in", it works just fine:

     Outlook.AddressEntry currentUser =
                  Application.Session.CurrentUser.AddressEntry;

    But in a Windows Form application, anytime I have that exact code (even though I have added references for Microsoft.Office.Interop.Outlook I always get a build error:

    Error 2 An object reference is required for the non-static field, method, or property

    Anytime I try to use :But I'm not using in a static class or method.  I can't seem to make Visual Studio happy.  I don't know if I'm being too vauge, I can post the entire codeblock if that's helpful.

    Microsoft.Office.Interop.Outlook.Application.Session.foo


    Thomas Kisner, MCITP -- http://thelync.net

    Sunday, April 29, 2012 5:04 AM

Answers

  • Hi Thomas,

    I assume you didn't initialize the Application variable of the Outlook.

    The following snippet works fine on my side. I hope it can help you.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Diagnostics;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;
    using Outlook = Microsoft.Office.Interop.Outlook;
    
    namespace WindowsFormsApplication11
    {
        public partial class Form1 : Form
        {
            private Outlook.Application OlApp = null;
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                Process[] ps = Process.GetProcessesByName("OUTLOOK");
                if (ps.Length > 0)
                {
                    OlApp = (Outlook.Application)Marshal.GetActiveObject("Outlook.Application");
                }
                else
                {
                    OlApp = new Outlook.Application();
                }
            }
    
            private void Form1_FormClosed(object sender, FormClosedEventArgs e)
            {
                Marshal.ReleaseComObject(OlApp);
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                Outlook.AddressEntry current_user = OlApp.Session.CurrentUser.AddressEntry;
                MessageBox.Show(current_user.Name);
            }
        }
    }

    @Dmitry,

    Thanks for you great work.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    Thursday, May 3, 2012 7:02 AM
    Moderator

All replies

  • Do you initialize the Application variable?


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.3 is now available!

    Sunday, April 29, 2012 7:03 AM
  • Hi Thomas,

    I assume you didn't initialize the Application variable of the Outlook.

    The following snippet works fine on my side. I hope it can help you.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Diagnostics;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;
    using Outlook = Microsoft.Office.Interop.Outlook;
    
    namespace WindowsFormsApplication11
    {
        public partial class Form1 : Form
        {
            private Outlook.Application OlApp = null;
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                Process[] ps = Process.GetProcessesByName("OUTLOOK");
                if (ps.Length > 0)
                {
                    OlApp = (Outlook.Application)Marshal.GetActiveObject("Outlook.Application");
                }
                else
                {
                    OlApp = new Outlook.Application();
                }
            }
    
            private void Form1_FormClosed(object sender, FormClosedEventArgs e)
            {
                Marshal.ReleaseComObject(OlApp);
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                Outlook.AddressEntry current_user = OlApp.Session.CurrentUser.AddressEntry;
                MessageBox.Show(current_user.Name);
            }
        }
    }

    @Dmitry,

    Thanks for you great work.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    Thursday, May 3, 2012 7:02 AM
    Moderator