Hi all,


The following code can popup a particular Outlook contact detail
Click button 1, it opens Tom Lee contact detail
Then, close the Tom Lee contact detail
Click button 2, it opens Sam Green contact detail
It works fine ...

However, if I am going to open both contact detail (without closing the other person contact)
it will come up with an error >>> "A dialog box is open. Close it and try again"

Any body knows how to solve it ?? 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 System.Threading;
using Outlook = Microsoft.Office.Interop.Outlook;

namespace testing1
{
    public partial class Form1 : Form
    {
        Outlook.Application oApp = new Outlook.Application();
        string firstName;
        string lastName;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
        
        private void FindContactEmailByName() {

            //firstName = args1;
            //lastName = args2;
            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)
        {
            firstName = "Tom";
            lastName = "Lee";
            popup();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            firstName = "Peter";
            lastName = "Green";
            popup();
        }
        void popup() {
            Thread th = new Thread(FindContactEmailByName);
            th.Start();
        }
    }
}

add an extra variable called shown

private bool shown = false;
private void button1_Click(object sender, EventArgs e)        
{            
firstName = "Tom";            
lastName = "Lee";            
popup();        
}         

private void button2_Click(object sender, EventArgs e)        
{           
 firstName = "Peter";            
lastName = "Green";            
popup();        
}       

 void popup() 
{            
if(!shown)
{
   shown = true;
   Thread th = new Thread(FindContactEmailByName);            
   th.Start();        
}
}

and when the form closes for the contact, don't see your code here, just mark shown = false;

If open the contact one by one... it is ok ~~
however, if open 2 or more contacts at the same time, it doesn't work ....

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.