locked
How to Create UI Automation for OutLook 2003 using System.Windows.Automation in C# .net 3.0 RRS feed

  • Question

  • Hi Can anyone Help,

    I am trying to do the following steps dynamically using UIAutomation
    in c# 3.0 with Visual studio 2005

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

    I have tried below code.I Can open the outlook 2003.But I can't open the Inbox read mail because I am getting Null value while reading the Inbox property condition.


    using System;
    
    using System.Collections.Generic;
    
    using System.Text;
    
    using System.Windows.Automation;
    
    using System.Threading;
    
    using System.Diagnostics;
    
    using System.Runtime.InteropServices;  
    
    namespace UITest 
    
    {
    
    
    
    class Program
    
    {
    
    
    
    [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
    
    {
    
    // pull up the existing outlook window
    
    IntPtr hWnd = processes[0].MainWindowHandle; 
    
    
    
    if (IsIconic(hWnd)) 
    
    {
    
    ShowWindowAsync(hWnd, SW_RESTORE);
    
    }
    
    SetForegroundWindow(hWnd);
    
    }
    
    
    
    if (aeOutLook != null) 
    
    {
    
    //Invoke Inbox
    
    AutomationElement aeInbox = FindAutomationElementByName(aeOutLook, "Inbox"); 
    
    
    
    if (aeInbox != null) 
    
    {
    
    proc1.WaitForInputIdle();
    
    InvokePattern ipNew = aeInbox .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

    • Moved by Jim Zhou - MSFT Wednesday, January 13, 2010 12:05 PM (From:Windows Presentation Foundation (WPF))
    Friday, January 8, 2010 2:53 PM

Answers

  • you might want to first find the pane in which the Inbox is in.. Once you find it then try finding the inbox...
    Also consider using TestAPI (http://testapi.codeplex.com/) for your automation. It has a bunch of useful helpers.
    Friday, January 8, 2010 3:38 PM

All replies

  • you might want to first find the pane in which the Inbox is in.. Once you find it then try finding the inbox...
    Also consider using TestAPI (http://testapi.codeplex.com/) for your automation. It has a bunch of useful helpers.
    Friday, January 8, 2010 3:38 PM
  • Hi Kuvanka,

    Since this is WPF forum and your issue is not related to WPF, so I am moving this thread to another forum for more ideas.

    Thanks.
    Sincerely.


    Jim Zhou -MSFT
    Wednesday, January 13, 2010 12:03 PM