Static void Main(string[] args)
{
EXCEL.Application aap = new EXCEL.Application();
string filelocation = "C:\\AdventureWorks_Sales.xlsx";
Workbook wrkbok = aap.Workbooks.Open(filelocation);
Worksheet sheet1 = wrkbok.ActiveSheet;
Range rng= sheet1.Range["A1","G10"];
Outlook.Application outlookapp = new Outlook.Application();
Outlook.NameSpace nameSpace = outlookapp.GetNamespace("MAPI");
nameSpace.Logon("", "",Missing.Value,Missing.Value);
Outlook.MailItem mail = (Outlook.MailItem)outlookapp.CreateItem(Outlook.OlItemType.olMailItem);
mail.Body = rng.PasteSpecial();
mail.To=("ddd@gmail.com");
mail.Send();
}
How can I paste the Excel range to the email body. As email body is string and below doesn't work
mail.Body = rng.PasteSpecial();
Instead of using a plain-text Body
property:
mail.Body = rng.PasteSpecial();
You can use two main ways for building the message body based on the Excel data. The first way is based on the HTMLBody
property which represents an HTML document. In that case you are responsible to build the HTML document structure based on the Excel data that you need to paste. See Paste Excel range to Outlook email body for more information.
But the most easiest solution is to use the Word object model. The WordEditor property returns the Microsoft Word Document Object Model of the message being displayed. So, you can paste directly to the document all the necessary data. See the Selection.Paste method which inserts the contents of the Clipboard at the specified selection. Using this method replaces the contents of the current selection. If you don't want to replace the contents of the selection, use the Collapse
method before using this method. When you use this method with a Range
object, the range expands to include the contents of the Clipboard
. When you use this method with a Selection
object, the selection does not expand to include the Clipboard
contents; instead, the selection is positioned after the pasted Clipboard
contents.