Kieron Thwaites

Hi,

I'm currently attemping an Excel 2007 plugin. Part of this plugin involves a button that, when clicked, launches the "send mail" window of Outlook and attaches the last saved copy of the spreadsheet as an attachment. (The user would then fill in the recipient address, message subject, etc. in the same way as sending an e-mail with Outlook.)

My problem is finding an example that would do this. My searches let me to this article, but I'm having difficulty converting it to C#. So, my questions:

  1. If the approach in the article I linked is correct, would someone assist me with getting the code to work with VSTO and C#
  2. If the approach in the article I linked is incorrect, would someone please provide a pointer to documentaion that explains the correct way of doing it

Thanks

--K



Re: Visual Studio Tools for Office Sending a copy of an Excel 2007 workbook as an attachment

Ji Zhou – MSFT

Hi Kieron,

Yes, you get the right codes example. But the C# version is little different from VB version. I modify it which works in my side. Hope the following codes can help: (You should add Outlook PIA as reference to your project)

Code Snippet

public void OnToggleButton1(Office.IRibbonControl control, bool isPressed)

{

if (!Globals.ThisAddIn.Application.ActiveWorkbook.Saved)

Globals.ThisAddIn.Application.ActiveWorkbook.Save();

object missing = Type.Missing;

Outlook.Application app = new Microsoft.Office.Interop.Outlook.Application();

Outlook.MailItem mail = app.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem) as Outlook.MailItem;

Outlook.Inspector insepector = mail.GetInspector;

insepector.Activate();

mail.To = "someone@microsoft.com";

mail.Subject = "Test";

mail.Body = "This is a test mail with workbook attached";

mail.Attachments.Add(Globals.ThisAddIn.Application.ActiveWorkbook.FullName, Outlook.OlAttachmentType.olByValue, missing, "Test Workbook");

}

OnToggleButton1 is the click event handle of a button on my ribbon.

Thanks

Ji