Search code examples
c#processoutlookminimize

How to start Outlook minimized?


I think starting a process minimized should be simple but I had no luck with outlook. How can I start Outlook minimized?

My attempt was this:

[DllImport("user32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);

static void Main(string[] args)
{
    ProcessStartInfo startInfo = new ProcessStartInfo();
    startInfo.FileName = "OUTLOOK.EXE";

    IntPtr hWnd = Process.Start(startInfo).Handle;

    bool state = false;
    if (!hWnd.Equals(IntPtr.Zero))
        state = ShowWindowAsync(hWnd, 2);

    // window values: http://msdn.microsoft.com/en-us/library/windows/desktop/ms633548(v=vs.85).aspx

    Console.WriteLine(state.ToString());
    Console.Read();
}

Solution

  • I have solved it but I like to hear your comments if you think the solution can be improved. I also have posted the solution on my blog with some more details at http://jwillmer.de/blog/2012/08/01/how-to-start-outlook-minimized-with-c/

    [DllImport("user32.dll")]
    private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
    
    // console application entry point
    static void Main()
    {
        // check if process already runs, otherwise start it
        if(Process.GetProcessesByName("OUTLOOK").Count().Equals(0))
            Process.Start("OUTLOOK");
    
        // get running process
        var process = Process.GetProcessesByName("OUTLOOK").First();
    
        // as long as the process is active
        while (!process.HasExited)
        {
            // title equals string.Empty as long as outlook is minimized
            // title starts with "öffnen" (engl: opening) as long as the programm is loading
            string title = Process.GetProcessById(process.Id).MainWindowTitle;
    
            // "posteingang" is german for inbox
            if (title.ToLower().StartsWith("posteingang"))
            {
                // minimize outlook and end the loop
                ShowWindowAsync(Process.GetProcessById(process.Id).MainWindowHandle, 2);
                break;
            }
    
            //wait awhile
            Thread.Sleep(100);
    
            // place for another exit condition for example: loop running > 1min
        }
    }