Automate Outlook and Word to create an email message

I am trying to create a Word email message that will be used as the body for an Outlook message. I looked at a sample code from Microsoft here , but this code crashes when I try to create a new word document in 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 Command Failed exception. I am using Office 2007 and have tried:

  • Link to the standard Office Word and Outlook libraries for Office 12
  • Install PIA Office XP and link to Office 12 libraries.
  • Install PIA Office XP and link to Office 2000 libraries.
  • Uninstall PIA Office XP and link to 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 will work. But I do not want a normal Word document, I need EmailDocument - using a different type of document causes an error when casting in Outlook.MailItemClass.

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

+1
source share

All Articles