none
A word document's contents as the body of an email meassage RRS feed

Answers

  • Hello Markand,

    Outlook doesn't support OpenXML. You need to use the Word object model to set the Body of e-mails messages. The MailItem class provides the GetInspector method which returns an Inspector object that represents an inspector initialized to contain the specified item. The Inspector class provides the WordEditor property which returns the Microsoft Word Document Object Model of the message being displayed, i.e. an instance of the Document class from the Word object model. You can customize the document on your own, for example, load an existing document and copy the required data into the e-mail's document or use OpenXML for opening an existing document and reading all values manually.

    Sub InsertBodyTextInWordEditor() 
     Dim myItem As Outlook.MailItem 
     Dim myInspector As Outlook.Inspector 
     'You must add a reference to the Microsoft Word Object Library 
     'before this sample will compile 
     Dim wdDoc As Word.Document 
     Dim wdRange As Word.Range 
     
     On Error Resume Next 
     Set myItem = Application.CreateItem(olMailItem) 
     myItem.Subject = "Testing..." 
     myItem.Display 
     'GetInspector property returns Inspector 
     Set myInspector = myItem.GetInspector 
     'Obtain the Word.Document for the Inspector 
     Set wdDoc = myInspector.WordEditor 
     If Not (wdDoc Is Nothing) Then 
     'Use the Range object to insert text 
     Set wdRange = wdDoc.Range(0, wdDoc.Characters.Count) 
     wdRange.InsertAfter ("Hello world!") 
     End If 
    End Sub
    

    Note, the WordEditor property is only valid if the IsWordMail method returns true and the EditorType property is olEditorWord.
    Wednesday, February 19, 2014 5:43 AM
  • Hi,

    There are three way we can set the body of en email via MailItem.Body, MailItem.HTMLBody and MailItem.RTFBody. And if the Word document only contains characters and you don’t want to get the format you can refer to the code below to get the text(Open XML):

    string fileName = @"C:\Users\User\Desktop\readContent.docx";
    
                // Create a word document by package. 
               using( WordprocessingDocument wordprocessingDocument =
                    WordprocessingDocument.Open(fileName,true))
               {
                    string bodyText = wordprocessingDocument.MainDocumentPart.Document.Body.InnerText;
                 } 
    
     

    And you can refer to code below to create a mailItem and set the body, and we we use Outlook Object model to send eamil, we need to install the Outlook.

    Application outLookAp = new Application();
                MailItem aMailItem = outLookAp.CreateItem(OlItemType.olMailItem);
                aMailItem.Subject = "test";
                aMailItem.Body = "";
                aMailItem.To="Person1@microsoft.com";
                aMailItem.Send();

    In addition, article bellows may be helpful:
    How Do I... (Outlook 2013 PIA Reference)
    Welcome to the Open XML SDK 2.5 for Office

    Best regards

    Fei

     


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, February 19, 2014 10:19 AM
    Moderator

All replies

  • Hello Markand,

    Outlook doesn't support OpenXML. You need to use the Word object model to set the Body of e-mails messages. The MailItem class provides the GetInspector method which returns an Inspector object that represents an inspector initialized to contain the specified item. The Inspector class provides the WordEditor property which returns the Microsoft Word Document Object Model of the message being displayed, i.e. an instance of the Document class from the Word object model. You can customize the document on your own, for example, load an existing document and copy the required data into the e-mail's document or use OpenXML for opening an existing document and reading all values manually.

    Sub InsertBodyTextInWordEditor() 
     Dim myItem As Outlook.MailItem 
     Dim myInspector As Outlook.Inspector 
     'You must add a reference to the Microsoft Word Object Library 
     'before this sample will compile 
     Dim wdDoc As Word.Document 
     Dim wdRange As Word.Range 
     
     On Error Resume Next 
     Set myItem = Application.CreateItem(olMailItem) 
     myItem.Subject = "Testing..." 
     myItem.Display 
     'GetInspector property returns Inspector 
     Set myInspector = myItem.GetInspector 
     'Obtain the Word.Document for the Inspector 
     Set wdDoc = myInspector.WordEditor 
     If Not (wdDoc Is Nothing) Then 
     'Use the Range object to insert text 
     Set wdRange = wdDoc.Range(0, wdDoc.Characters.Count) 
     wdRange.InsertAfter ("Hello world!") 
     End If 
    End Sub
    

    Note, the WordEditor property is only valid if the IsWordMail method returns true and the EditorType property is olEditorWord.
    Wednesday, February 19, 2014 5:43 AM
  • Hi,

    There are three way we can set the body of en email via MailItem.Body, MailItem.HTMLBody and MailItem.RTFBody. And if the Word document only contains characters and you don’t want to get the format you can refer to the code below to get the text(Open XML):

    string fileName = @"C:\Users\User\Desktop\readContent.docx";
    
                // Create a word document by package. 
               using( WordprocessingDocument wordprocessingDocument =
                    WordprocessingDocument.Open(fileName,true))
               {
                    string bodyText = wordprocessingDocument.MainDocumentPart.Document.Body.InnerText;
                 } 
    
     

    And you can refer to code below to create a mailItem and set the body, and we we use Outlook Object model to send eamil, we need to install the Outlook.

    Application outLookAp = new Application();
                MailItem aMailItem = outLookAp.CreateItem(OlItemType.olMailItem);
                aMailItem.Subject = "test";
                aMailItem.Body = "";
                aMailItem.To="Person1@microsoft.com";
                aMailItem.Send();

    In addition, article bellows may be helpful:
    How Do I... (Outlook 2013 PIA Reference)
    Welcome to the Open XML SDK 2.5 for Office

    Best regards

    Fei

     


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, February 19, 2014 10:19 AM
    Moderator