Hi all

I would like to popup an Outlook contact via a Click button. The "Popup action" works fine~
However, after the outlook contact comes up, the WinForm(Form1) seem stuck and hold ....
I can't move, minimize and maximize the WinForm ?????
Any body know what is the problem in here ??
Thanks

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Outlook = Microsoft.Office.Interop.Outlook;

namespace testing1
{
    public partial class Form1 : Form
    {
        Outlook.Application oApp = new Outlook.Application();
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
        private void FindContactEmailByName(string firstName, string lastName)
        {
            Outlook.NameSpace outlookNameSpace = oApp.GetNamespace("MAPI");
            Outlook.MAPIFolder contactsFolder = outlookNameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderContacts);

            Outlook.Items contactItems = contactsFolder.Items;

            try
            {
                Outlook.ContactItem contact = (Outlook.ContactItem)contactItems.Find(String.Format("[FirstName]='{0}' and " + "[LastName]='{1}'", firstName, lastName));
                if (contact != null)
                {
                    contact.Display(true);
                }
                else
                {
                    MessageBox.Show("The contact information was not found.");
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            FindContactEmailByName("Tom", "Lee");
        }

    }
}

Dani AI

Generated

Short version: the Outlook inspector is modal and your form is blocked. Calling the inspector with a modal flag will stop the thread that opened it; the Outlook object model also requires STA threading for UI-related calls. See the Microsoft docs for the Display method and Outlook threading notes. (learn.microsoft.com)

Practical options (ordered by simplicity):

  • Easiest: show the contact modeless instead of modal. Use Display(false) (or omit the parameter) so Outlook does not block the caller. This alone often fixes the “stuck” WinForm. (learn.microsoft.com)

  • If you must do the lookup off the UI thread, do it on a dedicated STA thread. Do not rely on BackgroundWorker or arbitrary thread-pool threads for COM/Outlook UI work: those threads are MTA by default and can fail with COM/STA errors. was right to suggest moving work off the UI thread, but BackgroundWorker uses thread-pool threads (MTA), so create a thread and set its apartment state to STA before starting it. (learn.microsoft.com)

Example pattern (skeleton — lookup code omitted):

var t = new Thread(() =>
{
    // create Outlook.Application on this STA thread,
    // perform the contact lookup, then show modeless inspector:
    // contact.Display(false);

    // cleanup COM objects explicitly:
    while (Marshal.ReleaseComObject(contact) > 0) ;
    while (Marshal.ReleaseComObject(outlook) > 0) ;
    GC.Collect(); GC.WaitForPendingFinalizers();
});
t.SetApartmentState(ApartmentState.STA);
t.IsBackground = true;
t.Start();

Also: explicitly release COM objects (Marshal.ReleaseComObject or FinalReleaseComObject and a GC run) to avoid leftover Outlook processes. And avoid server-side/service automation of Office — use a server-friendly API (EWS/Graph) if this runs unattended. (support.microsoft.com)

You need to run the contact launching code in a new thread, or consider using a background worker to start your FindContactEmailByName method.

Here's a tutorial on the background worker:

Basically, when you show a new form like that, the "owner" form that launches the contact.Display(true) call blocks until the contact is closed, or control is returned to the calling form.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.