Automating Outlook and Word to create email message

房东的猫 提交于 2019-12-24 06:37:21

问题


I'm trying to create a Word email message that will be used as the body for an Outlook message. I've been looking at sample code from Microsoft here but this code falls over when trying to create the new word document at the wordApp.Documents.Add line:

Word.Application wordApp = new Word.Application();

object template = System.Reflection.Missing.Value;
object newTemplate = System.Reflection.Missing.Value;
object documentType = Word.WdNewDocumentType.wdNewEmailMessage;
object visible = false;

Word.Document wordDoc = wordApp.Documents.Add(ref template, ref newTemplate, ref documentType, ref visible);

Outlook.MailItemClass mItem = (Outlook.MailItemClass)doc.MailEnvelope.Item;

I get a COM Exception of "Command Failed". I'm using Office 2007 and have tried:

  • Referencing the standard Office 12 Word and Outlook libraries
  • Installing the Office XP PIAs and referencing the Office 12 libraries
  • Installing the Office XP PIAs and referencing the Office 2000 libraries.
  • Uninstalling the Office XP PIAs and referencing the Office 2000 / Office 2007 libraries.

I get the same error every time. If I change the document type from wdNewEmailMessage to wdNewBlankDocument then it works. But I don't want a normal Word document I want an EmailDocument - using a differnt type of document throws error when casting to the Outlook.MailItemClass.

Ideally I would like the application to work on Word 2000 to Word 2007 ;-)

来源:https://stackoverflow.com/questions/1177224/automating-outlook-and-word-to-create-email-message

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!