简体   繁体   English

您如何使Outlook成为焦点并使用C#发送击键?

[英]How do you bring Outlook into focus and send it keystrokes in C#?

In my application I am trying to bring Outlook 2010 into focus and send it a CTRL-N (new email). 在我的应用程序中,我试图使Outlook 2010成为焦点,并向其发送CTRL-N(新电子邮件)。

I have tried many different iterations of ShowWindow, FindWindow, SetFocus, SetForegroundWindow and SendMessage and can't seem to get any of them to work. 我尝试了ShowWindow,FindWindow,SetFocus,SetForegroundWindow和SendMessage的许多不同迭代,但似乎无法使它们工作。

It works fine for Notepad, but not for Outlook... My code is: 它适用于记事本,但不适用于Outlook ...我的代码是:

    using System.Runtime.InteropServices;
    using System.Diagnostics;

    const int kKeyDown = 0x0100;
    const int kKeyUp = 0x0101;
    const int kCtrl = 0x11;
    const int kN = 0x4e;

    Process[] prcOutlook = System.Diagnostics.Process.GetProcesses();
      foreach (System.Diagnostics.Process prcTempProc in prcOutlook)
      {
          if (prcTempProc.ProcessName == "OUTLOOK")
          {
              IntPtr windowToFind = prcTempProc.MainWindowHandle;
              if (ShowWindow(windowToFind, 1))
              {
                  SetFocus(wHndle);
                  int result = SendMessage(windowToFind, kKeyDown, kCtrl, 0);
                  result = SendMessage(windowToFind, kKeyDown, kN, 0);
                  result = SendMessage(windowToFind, kKeyUp, kCtrl, 0);
                  result = SendMessage(windowToFind, kKeyUp, kN, 0);
              }
          }
      }

    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

The code runs fine, it just never brings Outlook to focus to get the keystrokes... 该代码运行良好,它从未使Outlook专注于获得击键...

Where am I going wrong? 我要去哪里错了?

Regards, Dean 问候,院长

Have a look at this question for a different approach to achieving the same result. 看一下这个问题,以不同的方式获得相同的结果。 You should also familiarize yourself with the Outlook PIA . 您还应该熟悉Outlook PIA

Don't try to control Outlook (or any other external application) by sending it keystrokes as if you are simulating a real user. 不要像发送模拟用户一样尝试通过发送击键来控制Outlook(或任何其他外部应用程序)。

For Outlook you can use COM interop . 对于Outlook,您可以使用COM互操作

A quick guide: 快速指南:

  1. Start a new project, a console application for instance. 启动一个新项目,例如一个控制台应用程序。
  2. Open the Add Reference dialog and select the COM tab 打开“添加引用”对话框,然后选择“ COM”选项卡
  3. Search for the Microsoft Outlook X Object Library (where X is the version) 搜索Microsoft Outlook X对象库(其中X是版本)
  4. Add a reference to it. 添加对其的引用。
  5. Add the namespace "Microsoft.Office.Interop.Outlook" to your using clauses. 将名称空间“ Microsoft.Office.Interop.Outlook”添加到您的using子句。

You can then execute the following code: 然后,您可以执行以下代码:

var application = new Application();
var mail = (_MailItem) application.CreateItem(OlItemType.olMailItem);

mail.To = "anonymous@somedomain.com";
// ... other mail properties ...

mail.Display(true);

First you start a new Outlook application. 首先,您启动一​​个新的Outlook应用程序。 Then you create a new mail item (_MailItem). 然后,您创建一个新的邮件项目(_MailItem)。 Use this object to configure the e-mail you want to send (to, from, subject...etc.) and then call its Display(...) method to show the Outlook new mail editor window. 使用此对象来配置要发送(到,从,主题等)的电子邮件,然后调用其Display(...)方法以显示Outlook新邮件编辑器窗口。

If you want to retrieve the e-mails from your inbox then execute the following code: 如果要从收件箱中检索电子邮件,请执行以下代码:

var ns = application.GetNamespace("MAPI");
MAPIFolder inbox = ns.GetDefaultFolder(OlDefaultFolders.olFolderInbox);
for (int i = 1; i <= inbox.Items.Count; i++)
{
    var item = (MailItem) inbox.Items[i];
    Console.WriteLine("Subject: {0}", item.Subject);
    //...
}

Let's take the first mail we find in the inbox: 让我们以收件箱中找到的第一封邮件为例:

var mailItem = (MailItem) inbox.Items[1];

You can then reply to the sender as follows: 然后,您可以按以下方式回复发件人:

var reply = mailItem.Reply();
reply.Display(true);

As you can see this is very similar to creating a new e-mail. 如您所见,这与创建新电子邮件非常相似。

A reply all is equally simple: 全部答复同样简单:

var replyAll = mailItem.ReplyAll();
replyAll.Display(true);

将窗口置于顶部后,尝试使用SendKeys.Send(^ N)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM