Visual Studio Developer Center > Visual Studio Forums > Visual Studio Tools for Office > How to use Outlook Add-in Application in other application?
Ask a questionAsk a question
 

AnswerHow to use Outlook Add-in Application in other application?

  • Sunday, January 13, 2008 3:06 AMmelegant Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Hi,

     

    So I have an application that I am writing that I want to attach a pdf to outlook etc etc.

     

    After a lot of research I know how to deal with the security pop's etc. So I created a VSTO Outlook add-in and proceeded to do something like this from within that app:

     

    private void ThisAddIn_Startup(object sender, System.EventArgs e)

    {

    Outlook.MailItem olMailItem = this.Application.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem) as Outlook.MailItem;

    Outlook.Recipient olRecp = olMailItem.Recipients.Add("Test@test.com");

    olMailItem.Display(true);

    }

     

     

    My question is, how can I setup my references in MY other application to achieve that lovely this.Application functionality? Or should I just do what I want in the add-in..then .dll it and ref it in my other application?

     

    Thanks!

    Melegant

Answers

All Replies

  • Sunday, January 13, 2008 2:40 PMSue Mosher - Outlook MVPMVP, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    That seems like the hard way of accomplishing your goal. Why not just have your application create the message and set its To value? There is no need to use Recipients.Add to create and display a simple message.
  • Sunday, January 13, 2008 11:51 PMmelegant Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I am trying to avoid the Outlook Security Guard or whatever it is called...by creating my own instance of Outlook.Applicaiton  it is untrusted...

     

  • Monday, January 14, 2008 12:13 PMSue Mosher - Outlook MVPMVP, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    My point is that no security prompts are ever involved in creating a message, setting its To property, and displaying it.
  • Monday, January 14, 2008 3:59 PMmelegant Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Thanks...I realize that. I would like to however be able to do some creative stuff with outlook that would require I get by those prompts...

     

    My question now is that I useded the VSTO to create an outlook add-in project which gave me a small class that exposes the 'Application' class that is trusted. EX:

     

    using System;

    using System.Windows.Forms;

    using Microsoft.VisualStudio.Tools.Applications.Runtime;

    using Outlook = Microsoft.Office.Interop.Outlook;

    using Office = Microsoft.Office.Core;

    namespace MitsicMail

    {

    public partial class MailLink

    {

    public MailLink(string Rec)

    {

    mRec = Rec;

    }

    public void InitMail(){

    InternalStartup();

    object n = new object();

    System.EventArgs ex = new EventArgs();

    ThisAddIn_Startup(n, ex);

    }

    private String mRec = "mgran@test.com";

    private string mBody = "Thank you for your Buisness!";

    public String MyRecipient { get { return mRec; } set { value = mRec; } }

    public String MessageBody { get { return mBody; } set { value = mBody; } }

    private void ThisAddIn_Startup(object sender, System.EventArgs e)

    {

    Outlook.MailItem olMailItem = this.Application.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem) as Outlook.MailItem;

    Outlook.Recipient olRecp = olMailItem.Recipients.Add("Test@test.com");

    olMailItem.Display(true);

    }

    private void ThisAddIn_Shutdown(object sender, System.EventArgs e)

    {

    }

    #region VSTO generated code

    /// <summary>

    /// Required method for Designer support - do not modify

    /// the contents of this method with the code editor.

    /// </summary>

    private void InternalStartup()

    {

    this.Startup += new System.EventHandler(ThisAddIn_Startup);

    this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);

    }

    #endregion

     

     

    now, when I run this, it works GREAT.

     

    Now, I compile, add to another project as a reference and when I create a new instance of THIS class it always errors saying that 'Application' is null. Argh why?

  • Monday, January 14, 2008 4:08 PMSue Mosher - Outlook MVPMVP, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    In that case, see http://blogs.msdn.com/johnrdurant/archive/2006/01/30/COMAddins-Collection.aspx, including the comments. The key step you're missing is that you need to specify what class the COMAddin.Object property exposes. That should not be the Outlook.Application object, unless you want to create a security vulnerability. Instead, the add-in should expose a class that contains the specific methods that you want your external application to use.
  • Monday, January 14, 2008 5:03 PMmelegant Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Thanks Sue..exactly what I was looking for! Now to see if I can make it work mwhahahah.

     

  • Saturday, June 28, 2008 7:34 AMvenkee Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Were you able to make it work without security promts?. I have been working on this more than a week Sad Thanks in Adavance.

     

  • Saturday, June 28, 2008 1:53 PMmelegant Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Hi,

    This is what I did: (and it works)

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

    object oMissing = System.Reflection.Missing.Value;

    olapp.Session.Logon(oMissing, oMissing, false, true);

    Microsoft.Office.Interop.Outlook.MailItem message = (Microsoft.Office.Interop.Outlook.MailItem)olapp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);

    try

    {

    message.Subject = "Hello";

    message.Body = "Hello, Please find our proposal attached. Have a great day!";

    message.To = tm.SQLReader.GetString(0); //Email

    int attachmentLocation = 1;

    message.Attachments.Add(expath, Microsoft.Office.Interop.Outlook.OlAttachmentType.olByValue, attachmentLocation, "Proposal");

    message.Display(false);

    System.IO.File.Delete(expath);

    }

     

    I also used the outlook security fix from  mapilab

    http://www.mapilab.com/support/articles/vb_outlook_security_1.html

     

    but I found that I did not need it on most clients using the above code.

     

    I hope this helps....