How to Create UI AUTOMATION for OUTLOOK 2007 using C# .Net 3.0.(using System.Windows.Automation;)

Unanswered How to Create UI AUTOMATION for OUTLOOK 2007 using C# .Net 3.0.(using System.Windows.Automation;)

  • Thursday, January 21, 2010 2:06 PM
     
      Has Code

    Can Any One Help,

    I am trying  UI AUTOMATION for OUTLOOK 2007 using C# .Net 3.0 (using System.Windows.Automation;)

    My Requirement's are mentioned below.

    1) Open Outlook 2007
    2) Open Inbox
    3) Select first unread email.
    4) Open the first unread email.
    5) If there are attachements, download the attachements.
    6) Select next unread email and do the same as above.

    I can open the OUTLOOK 2007, But I am getting NULL value while Searching Inbox using PropertyCondition.

    Here are the Code,

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Windows.Automation;
    using System.Windows.Automation.Provider;
    using System.Threading;
    using System.Diagnostics;
    using System.Runtime.InteropServices;
    using System.Windows;
    using System.Windows.Forms;
    
    namespace AmmeaBahavathi
    {
        class OutLookUIAutomation
        {
            [DllImport("user32.dll")]
            private static extern bool SetForegroundWindow(IntPtr hWnd);
            [DllImport("user32.dll")]
            private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
            [DllImport("user32.dll")]
            private static extern bool IsIconic(IntPtr hWnd);
            private const int SW_RESTORE = 9;                
    
            static void Main(string[] args)
            {           
                // look for outlook already running
                Process[] processes = Process.GetProcessesByName("outlook");
                AutomationElement aeOutLook = null;
                Process proc1 = new Process();
             
                if (processes.Length == 0)
                {
                    // start new outlook process              
                    proc1.StartInfo.FileName = "outlook";
                    proc1.Start();
                    proc1.WaitForInputIdle();
                    Thread.Sleep(5000);
                    aeOutLook = AutomationElement.FromHandle(proc1.MainWindowHandle);
                }
                else
                {
                    proc1.Close();
                    // pull up the existing outlook window
                    IntPtr hWnd = processes[0].MainWindowHandle;
                    if (IsIconic(hWnd))
                    {
                        ShowWindowAsync(hWnd, SW_RESTORE);
                    }
                    SetForegroundWindow(hWnd);
                }
                           
                
                if (aeOutLook != null)
                {
                    Thread.Sleep(1000);
    
                    //Invoke Inbox
                    AutomationElement aeNew = FindAutomationElementByName(aeOutLook, "Inbox");
                    if (aeNew != null)
                    {                          
                        Thread.Sleep(5000);
                        InvokePattern ipNew = aeNew.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern;
                        ipNew.Invoke();
                    }
                    else
                    {
                        Console.WriteLine("aeInbox:Null");
                    }
    
                }
                else
                {
                    Console.WriteLine("aeOutLook:Null");
                }
                Console.ReadLine();
            }
    
            public static AutomationElement FindAutomationElementByName(AutomationElement parent, string name)
            {
                PropertyCondition pc = new PropertyCondition(AutomationElement.NameProperty, name);
                return parent.FindFirst(TreeScope.Descendants, pc);            
            }      
        }
    }
    

    Thanks,
    Sivaprasad

All Replies

  • Friday, January 22, 2010 8:46 PM
    Owner
     
     

    Hi, Sivaprasad,

    Can you find it with the UISpy tool in the Windows SDK?  I tried that and found three instances of "Inbox", all of type Tree Item, but perhaps it might behave differently for you.

    Thanks,
    Michael


    This posting is provided "AS IS" with no warranties, and confers no rights.