1

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

Mo Patel
  • 2,321
  • 4
  • 22
  • 37
Chris B
  • 5,311
  • 11
  • 45
  • 57
  • What is your outlook and word version. Both 2007? – Shoban Jul 24 '09 at 12:15
  • Yes, both 2007. I did have a thought that it might be to do with priviliges as VS runs with elevated priviliges but running the executable directly has the same problem – Chris B Jul 24 '09 at 13:00
  • I just went into Word 2007 and tried to create a new email document and it seems the wdNewEmailMessage value no longer exists in 2007. I get wdTypeDocument, wdTypeFrameset and wdTypeTemplate. The above code doesn't work with any of these. Doncha just love Office :-( – Chris B Jul 24 '09 at 13:19
  • Does it *have* to be a word/outlook message? I have always found that working with the Office libraries hard work to get the correct versions. Personally (if this is ok for your circumstance) I'd favour using the Smtp mailer built into the framework and sending an HTML message from there. – The Senator Apr 15 '14 at 08:35

0 Answers0