我如何使用任何windows自动化技术向Outlook文本框添加一些文本?

问题描述 投票:0回答:1

我想在C#中使用任何windows自动化技术添加一些文本到Outlook的撰写邮件部分。它应该保留它的字体属性,意味着新输入的文本应该是用户在他的Outlook中定义的格式。

之前我使用的是 SendKeys.Send() 方法,但它并不可靠,因为他们不能确定文本是否会被生成。

我试着用 user32.dllSystem.Windows.Automation 但无法实现。

谁能帮帮我?一个测试用的小脚本会有很大的帮助。

先谢谢你了。

c# winforms automation outlook user32
1个回答
0
投票

你可以使用COM自动化技术来自动化Outlook中的任务。请看 通过使用Outlook对象模型实现Outlook自动化 MSDN中的部分。

设置Outlook项的正文主要有三种方式。

  1. Body属性(一个纯文本)。
  2. HTMLBody属性--允许使用HTML标记自定义正文,如上图所示。
  3. Word对象模型。Outlook默认使用Word作为电子邮件编辑器。检查器类的WordEditor属性返回一个代表正文的Document类的实例。

你可以在 第17章:使用项目机构.

正如你所看到的,只有最后两个允许完成你的任务。

例如,下面的示例代码使用Word对象模型在一个新创建的项目的消息体中插入一个文本字符串。

using Outlook = Microsoft.Office.Interop.Outlook;
using Word = Microsoft.Office.Interop.Word;
namespace CreateAndEditMailItemUsingWord
{
    class Program
    {
        static void Main(string[] args)
        {
            Outlook.MailItem mailItem = (new Outlook.Application()).CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
            Word.Document wordDocument = mailItem.GetInspector.WordEditor as Word.Document;
            // Insert the text at the very beginning of the document
            // You can control fonts and formatting using the ParagraphFormat propety of the Word.Range object
            Word.Range wordRange = wordDocument.Range(0, 0);
            wordRange.Text = "Please insert your text here";
            mailItem.Display();
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.