none
Re Question]Copy Excel Range and Paste into an Outlook Email. RRS feed

  • Question

  • I read below this link.. Thanks Bruce Song...

    http://social.msdn.microsoft.com/Forums/en-US/outlookdev/thread/5248288a-0f86-4fe7-8235-2ccb85d2b014/#c50e51e1-87c2-4af9-9195-e0c61eb32bc8

    I created this code as a test code.
    Please, check the code whether it’s good or not.
    And let me know the part I can do more optimal and then please correct my code to better code.
    Lastly, which is better way among below methods?

    1. using Word = Microsoft.Office.Interop.Word; And  Add referencee

      private static void sendMail()
        {
    
          Outlook.Application app = new Outlook.Application();
          Outlook.MailItem mailitem = (Outlook.MailItem)app.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);   
    
          mailitem.Subject = Globals.Sheet3.Range["a1", Type.Missing].Text.ToString();
          mailitem.To =  Globals.Sheet3.VSTOMAIL.Value2.ToString();      
    
          mailitem.Display(false);     
          Outlook.Inspectors insp = app.Inspectors;
          Outlook.Inspector ins = insp[1];
          object s = 1;
          object x = Word.WdUnits.wdStory;
          Word.Document doc = (Word.Document)ins.WordEditor;
          Word.Selection sel = doc.Application.Windows.get_Item(ref s).Selection;
          sel.Move(ref x,ref s);
          sel.PasteAndFormat(Microsoft.Office.Interop.Word.WdRecoveryType.wdFormatOriginalFormatting);         
          ((Outlook._MailItem)mailitem).Send();
          mailitem = null;
          app = null;
          Clipboard.Clear();
          sel = null;
          doc = null;
    
        }
    

    2.

     private static void sendMail()
     {
      Outlook.Application app = new Outlook.Application();
      Outlook.MailItem mailitem = (Outlook.MailItem)app.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
      mailitem.Subject = Globals.Sheet3.Range["a1", Type.Missing].Text.ToString();
      mailitem.To = Globals.Sheet3.VSTOMAIL.Value2.ToString();
      Globals.ThisWorkbook.PublishObjects.Add(Microsoft.Office.Interop.Excel.XlSourceType.xlSourceRange, @"C:\kesung\test.htm",
      Type.Missing, "testa", Excel.XlHtmlType.xlHtmlStatic, Type.Missing, Type.Missing).Publish(true);
      StreamReader sr = new StreamReader(@"C:\kesung\test.htm", Encoding.Default);
      mailitem.HTMLBody = sr.ReadToEnd();
      mailitem.Display(false);
      ((Outlook._MailItem)mailitem).Send();
      mailitem = null;
      app = null;
     }
    

    http://vsto.tistory.com
    Tuesday, May 24, 2011 6:24 AM

Answers

  • Hi VSTO_Beginner,

    I have tested both two methods and they work well, but if I have already open the outlook application, the first approach will encounter a COMException error:

    "This method or property is not available because the document is locked for editing." So, I suggest use the second method even the outlook application has started, it can run well.

    Hope this can give you the hint.

    Best Regards,


    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, May 27, 2011 6:52 AM