none
The GetSelectNamesDialog popup appears at the background when the Outllok is opedned. How to bring it to the front (Foreground or Topmost) ? RRS feed

  • Question

  • The Popup opens from the below code appears in the background, How to make the popup appear foregroung(Topmost) ?  

     Microsoft.Office.Interop.Outlook.Application Application = new Microsoft.Office.Interop.Outlook.Application();
     Microsoft.Office.Interop.Outlook.SelectNamesDialog snd =Application.Session.GetSelectNamesDialog();
     snd.InitialAddressList = Application.Session.GetGlobalAddressList();
     snd.Display();

    • Moved by Yang,Chenfei Friday, February 21, 2014 1:52 AM Outlook related
    Thursday, February 20, 2014 3:19 PM

Answers

  • As I said, there is no Outlook active window to display. You should display either an Explorer or Inspector. Let's say you want to display an Outlook Inspector for a mail item.

    Microsoft.Office.Interop.Outlook.Application oOutlook = new Microsoft.Office.Interop.Outlook.Application();
                Microsoft.Office.Interop.Outlook.NameSpace oNS = oOutlook.GetNamespace("MAPI");
                oNS.Logon("", "", false, false);

    Microsoft.Office.Interop.Outlook.MailItem mail = oOutlook.CreateItem(0);

    mail.Display(false); // opens an Outlook window, now display dialog.

                Microsoft.Office.Interop.Outlook.SelectNamesDialog snd = oNS.GetSelectNamesDialog();
                snd.InitialAddressList = oNS.GetGlobalAddressList();
                snd.Display();


    Ken Slovak MVP - Outlook

    Friday, February 21, 2014 6:06 PM
    Moderator

All replies

  • Hi,

    I will move this thread to Outlook for Developers forum. Where you can contact many experts about this topic.

    Have a nice time!

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, February 21, 2014 1:52 AM
  • Hello logesh,

    The Display method of the SelectNamesDialog class from the Outlook object model doesn't allow to specify the parent window handle because it is based on Outlook windows and always is shown as a modal window, meaning that code execution will halt until the user clicks OK, Cancel, or the close icon.

    As a workaround I can propose two possible solutions:

    1. Do some Windows API programming to find the handle and bring it to the front.

    2. Create your own form and then display it specifying the parent window. The Show and ShowDialog methods of the System.Windows.Forms.Form class allows you to specify the IWin32Window interface which represents a parent window.

    Friday, February 21, 2014 8:56 AM
  • I would display Outlook user interface before displaying the SelectNamesDialog. Try displaying an Explorer or Inspector, and logging into the session before using it.

    Use GetNameSpace("MAPI") to get your session reference, then use .Logon("", "", false, false); to log on before displaying the dialog. It's modal to Outlook and if an Outlook window isn't displayed the z-order won't be correct.


    Ken Slovak MVP - Outlook

    Friday, February 21, 2014 3:12 PM
    Moderator
  • We open the SND from a button click within Outlook.  As such, Outlook is already running and has the "window focus."  When the SND is displayed, it is done so Modally (to the Outlook application.)

    From where is your code executing?

    Your code suggests creating a new application object which may not be visible or in the foreground.  You may need to add some code to display Outlook, before displaying the SND.  Something like:

    Application.ActiveWindow.Activate

    This is indeed a problem others may have had.

    Friday, February 21, 2014 3:25 PM
  • In C#,  I have Activate function but it is not working. 

    If I Specify Application.ActiveWindow.Activate, the below error comes

    Error 1 Only assignment, call, increment, decrement, and new object expressions can be used as a statement 

    Error 2 'System.Windows.Application' does not contain a definition for 'ActiveWindow'

    Friday, February 21, 2014 4:11 PM
  • Thank you very much for the suggestion

    Can you please provide some code in C#, so that it will help me in solving the issue ?

    Friday, February 21, 2014 4:12 PM
  • Thank you very much for the suggestion

    Can you please provide some Windows API programming code in C#, so that it will help me in solving the issue ?

    Friday, February 21, 2014 4:14 PM
  • You would need to get the Outlook.Application object, not the application the code is running in. Also, unless you display Outlook user interface (Explorer or Inspector) there won't be an ActiveWindow to activate.

    If what you now have isn't working then show your current code.


    Ken Slovak MVP - Outlook

    Friday, February 21, 2014 4:57 PM
    Moderator
  • Hello Logesh,

    You can use the FindWindow function to find the dialog window handle. Then the BringWindowToTop function can be used for bringing the specified window to the top of the Z order.

    You can find a sample code in the How To Bring an Application Window to the Top with Focus article.

    Friday, February 21, 2014 4:59 PM
  • Hi Ken, 

    The line of code (Application.ActiveWindow.Activate;) in the below code shows error. How to display Outlook user inferface (Explorer or Inspector)?

    Microsoft.Office.Interop.Outlook.Application Application = new Microsoft.Office.Interop.Outlook.Application();
    Application.ActiveWindow.Activate;

    Microsoft.Office.Interop.Outlook.SelectNamesDialog snd =Application.Session.GetSelectNamesDialog();
     snd.InitialAddressList = Application.Session.GetGlobalAddressList();
     snd.Display();

    Friday, February 21, 2014 5:35 PM
  • Hi Ken,

    I have also tried your suggestion. Still the popup stays behind if the outlook is open.

    Microsoft.Office.Interop.Outlook.Application oOutlook = new Microsoft.Office.Interop.Outlook.Application();
                Microsoft.Office.Interop.Outlook.NameSpace oNS = oOutlook.GetNamespace("MAPI");
                oNS.Logon("", "", false, false);
                Microsoft.Office.Interop.Outlook.SelectNamesDialog snd = oNS.GetSelectNamesDialog();
                snd.InitialAddressList = oNS.GetGlobalAddressList();
                snd.Display();

    Friday, February 21, 2014 5:37 PM
  • As I said, there is no Outlook active window to display. You should display either an Explorer or Inspector. Let's say you want to display an Outlook Inspector for a mail item.

    Microsoft.Office.Interop.Outlook.Application oOutlook = new Microsoft.Office.Interop.Outlook.Application();
                Microsoft.Office.Interop.Outlook.NameSpace oNS = oOutlook.GetNamespace("MAPI");
                oNS.Logon("", "", false, false);

    Microsoft.Office.Interop.Outlook.MailItem mail = oOutlook.CreateItem(0);

    mail.Display(false); // opens an Outlook window, now display dialog.

                Microsoft.Office.Interop.Outlook.SelectNamesDialog snd = oNS.GetSelectNamesDialog();
                snd.InitialAddressList = oNS.GetGlobalAddressList();
                snd.Display();


    Ken Slovak MVP - Outlook

    Friday, February 21, 2014 6:06 PM
    Moderator
  • Thanks Ken for the help and code.

     The code works fine but the code opens a new mailItem to send email. Can we open the modal dialog box (GetSelectNamesDialog) in the foreground without opening a Compose new mail box.

     

    Friday, February 21, 2014 6:40 PM
  • Thanks for the help
    Friday, February 21, 2014 6:51 PM
  • You can open an Explorer window (an Outlook folder window) if you don't want an Inspector (item window).

    If you don't want to do that use Spy++ to find the window handle and caption for the Outlook dialog when it's displayed. Use that information with PInvoke calls to the Win32 API to FindWindow() and then BringWindowToTop() or SetWindowPos() with a WM_TOPMOST argument, depending on the parent/child relationship of the windows to your code.


    Ken Slovak MVP - Outlook

    Friday, February 21, 2014 6:58 PM
    Moderator
  • Hi Ken,

              The code works fine in  32-bit pc but in 64-bit machine the popup appears at the background when the Outllok is opened. Please help me

    Microsoft.Office.Interop.Outlook.Application oOutlook = new Microsoft.Office.Interop.Outlook.Application();
                Microsoft.Office.Interop.Outlook.NameSpace oNS = oOutlook.GetNamespace("MAPI");
                oNS.Logon("", "", false, false);
                Microsoft.Office.Interop.Outlook.SelectNamesDialog snd = oNS.GetSelectNamesDialog();
                snd.InitialAddressList = oNS.GetGlobalAddressList();


                Process[] process = Process.GetProcessesByName("OUTLOOK");

                if (process.Length > 0)
                {
                    if (process[0].MainWindowTitle != "")
                    {
                        Microsoft.Office.Interop.Outlook.MailItem mail = oOutlook.CreateItem(0);
                        mail.Display(false);
                        mail.Close(Microsoft.Office.Interop.Outlook.OlInspectorClose.olDiscard);
                    }
                }
                snd.Display();

    Friday, February 28, 2014 8:21 PM
  • Why are you displaying a mailitem and then closing it before you display the dialog? Once you close the Inspector you don't have any Outlook UI any more.

    If you insist on doing that, you need to follow the other advice you were given.

    Use Spy++ to get the caption and window class of the dialog and use that information with PInvoke calls to the Win32 API to FindWindow() and then BringWindowToTop() or SetWindowPos() with a WM_TOPMOST argument, depending on the parent/child relationship of the windows to your code.


    Ken Slovak MVP - Outlook

    Friday, February 28, 2014 8:28 PM
    Moderator