Automating Outlook and Word to create email messag

2019-07-26 05:46发布

问题:

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 ;-)