Programmatically sending "enter" keystroke to Outlook

This is a discussion on Programmatically sending "enter" keystroke to Outlook within the C# Programming forums, part of the General Programming Boards category; I'm working on a project where my process needs to send a series of "enter" keystrokes to a running instance ...

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    5

    Programmatically sending "enter" keystroke to Outlook

    I'm working on a project where my process needs to send a series of "enter" keystrokes to a running instance of MS Outlook 2003. The running instance was invoked by my program and I have an instance of Outlook.ApplicationClass at my disposal.

    Can anyone point me in the direction of a tutoral which would show me how to do this? I haven't been able to find anything in MSDN which tells me what I need to know, or even a very good description of the Outlook namespace.

    Any ideas?

    Thanks,
    Zak

  2. #2
    Registered User
    Join Date
    Aug 2004
    Posts
    5
    Figured out that I can simulate keyboard input using

    SendKeys.SendWait("{ENTER}");

    But Outlook is still not receiving the message. I think this is because my app is sending the keystrokes before Outlook it ready receive them. I thought that I had seen something about Outlook.Application having a function which would report the status of whether Outlook is ready to receive user input. Anyone know of any documentation on this? Am I barking up the wrong tree?

    Thanks,
    Zak

  3. #3
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    Maybe there is another way instead of simulating user input by sending keys. What do you want to achieve ?
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  4. #4
    Registered User
    Join Date
    Aug 2004
    Posts
    5
    Well, what I'm trying to do is this:

    I do a lot of testing where I need to start with a clean Outlook.pst. If you delete outlook.pst, Outlook will generate a new one when you start it up. It does require 3 "enter" keystrokes to complete the process, though, and I wanted to automate that.... Any idears?

    I can easily delete outlook.pst, start Outlook and quit Outlook programatically... I just cant figure out how to get the "enter" keystrokes to Outlook. Either that or I need a fundamentally differrent approach to accomplishing what I want to do!

    Thanks,
    Zak

  5. #5
    Registered User
    Join Date
    Aug 2004
    Posts
    5
    Well if I manually make my application pause long enough for Outlook to be receptive to user input, it works just fine.

    So, the concept is there, I just want to make it a little more elegant and dynamically determine when Outlook is ready for input. I'd appreciate it if anyone could help me on this matter...

    Thanks,
    Zak

  6. #6
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    Maybe the WaitForInputIdle method of the process instance is what you are looking for. I have not used it yet, but it looks as if it fits your needs.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  7. #7
    Registered User
    Join Date
    Aug 2004
    Posts
    5
    nvoigt, Worked great. Thanks! Just set up a loop to repeatedly invoke WaitForInputIdle on the Outlook process until we get true in response.

    However I have found a new problem.... When I run my app in debug everything is fine and dandy, but if I run it without debugging Outlook loses focus to my application and my SendKeys created CRLF's are received by the wrong app.

    So I did some research and the best thing I have found is a somewhat kludgey way to make sure that Outlook has focus when I sent the CRLFs. I got this technique from http://www.bobpowell.net/tipstricks.htm. Adapted to my code it looks like this:

    Code:
    using System.Runtime.InteropServices;
    
    [DllImport("User32.dll")]
    public static extern int ShowWindowAsync(IntPtr hWnd , int swCommand);
    
    .
    .
    .
    
    ShowWindowAsync(theOutlookProcessInstance.MainWindowHandle, 2)
    ShowWindowAsync(theOutlookProcessInstance.MainWindowHandle, 9)
    This should theoretically minimize and then restore the window defined by the hWnd arg of the function.

    The problem is that get the following compile error:
    Compiler Error CS1518: Expected class, delegate, enum, interface, or struct

    The squiggly line in the code indicating where the error is, is unter the return value in the ShowWindowAsyc function declaration.

    I have tried importing other dll's and declaring other functions and get the same error, so its not particular to this DLL or function. The syntax I see in other similar examples on the web is identical. Anyone have a guess? Maybe I should start a new thread on this problem....

    Thanks again for your help!

    Zak

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sending keystroke to a program
    By mvs in forum Windows Programming
    Replies: 1
    Last Post: 08-03-2006, 08:37 AM
  2. Sending Keystroke to HWND
    By loko in forum Windows Programming
    Replies: 6
    Last Post: 01-27-2005, 02:22 AM
  3. Familar with Outlook Express?
    By Shadow in forum Tech Board
    Replies: 1
    Last Post: 05-22-2004, 02:45 PM
  4. Replies: 8
    Last Post: 09-22-2003, 02:31 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21