我想在C#中使用任何windows自动化技术添加一些文本到Outlook的撰写邮件部分。它应该保留它的字体属性,意味着新输入的文本应该是用户在他的Outlook中定义的格式。
之前我使用的是 SendKeys.Send() 方法,但它并不可靠,因为他们不能确定文本是否会被生成。
我试着用 user32.dll 和 System.Windows.Automation 但无法实现。
谁能帮帮我?一个测试用的小脚本会有很大的帮助。
先谢谢你了。
你可以使用COM自动化技术来自动化Outlook中的任务。请看 通过使用Outlook对象模型实现Outlook自动化 MSDN中的部分。
设置Outlook项的正文主要有三种方式。
你可以在 第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();
}
}
}