Click here to Skip to main content
15,897,704 members
Articles / Productivity Apps and Services / Microsoft Office

Customize the built-in Outlook Select Names dialog (or any other)

Rate me:
Please Sign up or sign in to vote.
4.90/5 (35 votes)
14 Jan 2015CPOL14 min read 167.7K   2.6K   67  
In this article, you will learn how to customize the built-in Select Names dialog and use different external data sources for your own dialog.
using System;
using System.Collections.Generic;
using Outlook = Microsoft.Office.Interop.Outlook;
using Office = Microsoft.Office.Core;

namespace CustomOutlookDialog {

    internal class InspectorWrapper {

        public event EventHandler<Guid> InspectorClosed;

        public Guid ID { get; private set; }

        public Outlook.Inspector Inspector { get; private set; }

        public InspectorWrapper(Outlook.Inspector inspector) {
            Inspector = inspector;
            ConnectEvents();
        }

        void ConnectEvents() {
            ((Outlook.InspectorEvents_10_Event)Inspector).Close += new Outlook.InspectorEvents_10_CloseEventHandler(InspectorWrapper_Close);
            ((Outlook.InspectorEvents_10_Event)Inspector).Activate += new Outlook.InspectorEvents_10_ActivateEventHandler(InspectorWrapper_Activate);
            ((Outlook.InspectorEvents_10_Event)Inspector).Deactivate += new Outlook.InspectorEvents_10_DeactivateEventHandler(InspectorWrapper_Deactivate);
        }

        void DisconnectEvents() {
            ((Outlook.InspectorEvents_10_Event)Inspector).Close -= new Outlook.InspectorEvents_10_CloseEventHandler(InspectorWrapper_Close);
            ((Outlook.InspectorEvents_10_Event)Inspector).Activate -= new Outlook.InspectorEvents_10_ActivateEventHandler(InspectorWrapper_Activate);
            ((Outlook.InspectorEvents_10_Event)Inspector).Deactivate -= new Outlook.InspectorEvents_10_DeactivateEventHandler(InspectorWrapper_Deactivate);
        }

        void InspectorWrapper_Close() {
            DisconnectEvents();

            Inspector = null;
            GC.Collect();
            GC.WaitForPendingFinalizers();

            // inform the application to release al references.
            if (InspectorClosed != null) InspectorClosed(this, ID);
        }

        private IntPtr _invisibleWindow;

        private void InspectorWrapper_Activate() {
            if (_invisibleWindow != IntPtr.Zero) {

                // Close the invisible window
                WinApiProvider.PostMessage(_invisibleWindow, WinApiProvider.WM_CLOSE, 0, 0);

                FormRecipientDialog customDialog = new FormRecipientDialog(Inspector.CurrentItem);
                customDialog.ShowDialog();
            }
        }


        void InspectorWrapper_Deactivate() {
            int languageId = Inspector.Application.LanguageSettings.get_LanguageID(Microsoft.Office.Core.MsoAppLanguageID.msoLanguageIDUI);
            _invisibleWindow = WinApiProvider.SearchForAddressbookWindow(languageId);
        }


    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) X4U electronix
Germany Germany
I'm a 1968 model, made in Germany.
After School transformed into an electronic engineer and started repairing computers in 1986. From PET till now In the IT-world. Currently employed as softwaredeveloper in a company creating solutions for pharmacies.
MCSE2000, MCSD, MCTS - Team Foundation Server, MCTS - Visual Studio Tools for Office.
Specialized in Outlook/Exchange custom development. Languages: german, english, C#, C++, VB.Net, VB6, SQL.
In 2006 received an award as MVP - Visual Developer VSTO by Microsoft.
Homepage: [http://www.x4u.de]

Comments and Discussions