none
How to open the Insert Outlook-Item dialogue of Outlook through VSTO Outlook Addin? RRS feed

  • Question

  • Hi,

    I need to open the Insert Outlook-Item dialogue of Outlook  through VSTO Outlook Addin.
    Is there any way to open it and get the selected items back?


    -- Happiness is only real when shared :)

    Wednesday, June 27, 2012 5:06 AM

Answers

  • ActiveInspector.CommandBars.ExecuteMso ("AttachItem")

    • Marked as answer by Vipul Arwade Wednesday, June 27, 2012 10:39 AM
    • Unmarked as answer by Vipul Arwade Wednesday, June 27, 2012 10:39 AM
    • Marked as answer by Vipul Arwade Wednesday, June 27, 2012 10:40 AM
    Wednesday, June 27, 2012 7:43 AM
  • in explorer? no, outlook would not know where to insert selected item. What i presented you was the possibility to invoke built-in command in context that it expects. What you want is to have the same window but use it in your own context. Such operations are not supported, you would have to create your own window that looks like that one.
    • Marked as answer by Vipul Arwade Wednesday, June 27, 2012 12:54 PM
    Wednesday, June 27, 2012 11:40 AM

All replies

  • ActiveInspector.CommandBars.ExecuteMso ("AttachItem")

    • Marked as answer by Vipul Arwade Wednesday, June 27, 2012 10:39 AM
    • Unmarked as answer by Vipul Arwade Wednesday, June 27, 2012 10:39 AM
    • Marked as answer by Vipul Arwade Wednesday, June 27, 2012 10:40 AM
    Wednesday, June 27, 2012 7:43 AM
  • Hi DamianD,

    Thanks for reply.
    The given code opens the "insert dialouge" when Inspector window is open.
    But I want to open "Insert Outlook-Item dialogue" through button inside the VSTO Outlook Addin and Addin is inside the "Explorer" window so Inspector object is null.
    Is this possible to open "Insert Outlook-Item dialogue" in Explorer window(when Inspector window is not opened)?
    And also want to get the selected outlook items inside the 'insert Dialouge'.
    Please advise.


    -- Happiness is only real when shared :)

    Wednesday, June 27, 2012 10:40 AM
  • in explorer? no, outlook would not know where to insert selected item. What i presented you was the possibility to invoke built-in command in context that it expects. What you want is to have the same window but use it in your own context. Such operations are not supported, you would have to create your own window that looks like that one.
    • Marked as answer by Vipul Arwade Wednesday, June 27, 2012 12:54 PM
    Wednesday, June 27, 2012 11:40 AM
  • oohh...then I will try to create own window to insert outlook items in Exploer.
    thanks a lot DamianD :)

    -- Happiness is only real when shared :)

    Wednesday, June 27, 2012 12:48 PM
  • You can use a FileOpen dialog to provide a dialog to the user, then just read back the selection the user made. You'd need to reference an Outlook item, probably from Explorer.Selection[1] to attach the selected file. You would get the MailItem reference and use the Attachments.Add() method for that item.

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "Vipul Arwade" <=?utf-8?B?VmlwdWwgQXJ3YWRl?=> wrote in message news:85632e9d-657f-456c-9d1d-5c9ffd08abc1...
    oohh...then I will try to create own window to insert outlook items in Exploer.
    thanks a lot DamianD :)

    -- Happiness is only real when shared :)


    Ken Slovak MVP - Outlook
    Wednesday, June 27, 2012 2:20 PM
  • Hi Damian, I'm novice in outlook development. Could you explain please?
    Monday, August 19, 2013 3:39 PM
  • Hi,
    I’m working on an addin in order to insert an existing outlook item(mails), as attachment. I want to provide to users the possibility to choose an existing message with the insert outlook items dialogue. do you know how to open the insert items dialogue.
    Thanks for help
    Wednesday, August 21, 2013 8:43 AM
  • Please don't hijack other user's threads.

    In any case, your answer is a couple of posts up in this thread. With an open item (Inspector) call the ActiveInspector.CommandBars.ExecuteMso ("AttachItem") method. That will open the insert dialog.


    Ken Slovak MVP - Outlook

    • Proposed as answer by suki_11 Thursday, August 22, 2013 11:33 AM
    • Unproposed as answer by suki_11 Thursday, August 22, 2013 11:33 AM
    Wednesday, August 21, 2013 2:16 PM
  • Sorry!!

    Thanks for your response.I tried this, but unfortunately, it doesn't work :/

    Outlook.Application OutlookApplication = new Outlook.Application();
    OutlookApplication.ActiveInspector().CommandBars.ExecuteMso("AttachItem");

    Thanks for replying

    Thursday, August 22, 2013 11:35 AM
  • Never create a new Outlook Application object within an Outlook addin. Use the trusted Application passed to you in the VSTO startup event handler.

    Is an item open when you use that code? An Inspector is the window for open items. ActiveInspector is the active item window. If no items are open there are no Inspectors.

    FWIW, saying something doesn't work doesn't help. It can mean almost anything from "Outlook crashed" to "there was an exception" to "nothing happened" or just about anything else. Always provide enough description so someone else can figure out what you're talking about.


    Ken Slovak MVP - Outlook

    Thursday, August 22, 2013 2:25 PM
  • Hi Ken,

    Sorry again !!! I'm beginner in outlook addin, and there are many points that I don't understand. Thanks for providing your knowledge. I add a button "add attachment" only for the opened item, with the TabReadMessage idMso. I want to open the insert items dialog when the user click on it. I've got a method OnMyButtonAttachment() in the Ribbon.cs. here the code:

     public void OnMyButtonAttachment(Office.IRibbonControl control)
            {
                Outlook.MailItem email = null;
                try
                {
                    if (control.Context is Outlook.Inspector)
                    {
                        email = olApplication.ActiveInspector().CurrentItem;
                        olApplication.ActiveInspector().CommandBars.ExecuteMso("AttachItem");
                        
                    }
                }
                catch (SystemException ex)
                {
                    System.Windows.Forms.MessageBox.Show(ex.Message);
                }

    }

    and in ThisAddins.cs:

    public partial class ThisAddIn
        {
            #region Instance Variables
            Outlook.Application m_Application;
            
            internal static Office.IRibbonUI m_Ribbon;
            
          

            #endregion

            private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {
                
            }
           
            protected override Microsoft.Office.Core.IRibbonExtensibility CreateRibbonExtensibilityObject()
            {
                return new Ribbon(m_Application);
            }
     
            private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
            {
            }

            
            #region Code généré par VSTO

            /// <summary>
            /// Méthode requise pour la prise en charge du concepteur - ne modifiez pas
            /// le contenu de cette méthode avec l'éditeur de code.
            /// </summary>
            private void InternalStartup()
            {
                this.Startup += new System.EventHandler(ThisAddIn_Startup);
                this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
            }
            
            #endregion
        }

    What should I do? hope that I explained it clearly. Actually, with this code, I've this exception: "La référence d'objet n'est pas définie à une instance d'objet" it's a NullreferenceException

    Thanks for replying

    Friday, August 23, 2013 4:50 AM
  • You would need to set the class-level Application object, as it is now it remains as an uninitialized object.

    private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {
                m_Application = this.Application;
            }

    Then in your click handler (I'm assuming that it is a button click handler), something like this:

     public void OnMyButtonAttachment(Office.IRibbonControl control)
            {
                // Outlook.MailItem email = null;
                try
                {
                    if (control.Context is Outlook.Inspector)
                    {
                        // email = olApplication.ActiveInspector().CurrentItem;
                        ((Outlook.Inspector)control.Context).CommandBars.ExecuteMso("AttachItem");
                        
                    }
                }

    I'm not sure what CreateRibbonExtensibilityObject() is supposed to be doing or why.

    I'm also not sure how you're creating your ribbon UI and if your ribbon UI is being instantiated and called.


    Ken Slovak MVP - Outlook

    Friday, August 23, 2013 3:12 PM
  • Hi Ken,

    Thank you very much for replying. CreateRibbonExtensibilityObject() allows to display the ribbon, 'cause I used the ribbon designer at the beginning. And if I remove it, the ribbon disapears. I follow the step that you explain, but I 've got an error code 0x80004005 :( I think it's a technical problem.Someone could help me please?
    Monday, August 26, 2013 7:24 AM
  • That error is usually an Access Denied error. For anyone to be able to help at all you need to provide information.

    Where is this error occurring? On what line of code?

    What changes did you make?

    Are you stepping your code in the debugger to make sure all objects you need are instantiated and aren't null?

    Show the code you're now using and indicate where the error occurs.


    Ken Slovak MVP - Outlook

    Monday, August 26, 2013 5:33 PM
  • this is the new code

     public partial class ThisAddIn
        {
            #region Instance Variables
            Outlook.Application m_Application;
            
            internal static Office.IRibbonUI m_Ribbon;

            #endregion

            private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {
                m_Application = this.Application;
            }

            protected override Microsoft.Office.Core.IRibbonExtensibility CreateRibbonExtensibilityObject()
            {
                return new Ribbon(m_Application);
            }
     
            private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
            {
            }

            #region Code généré par VSTO

            /// <summary>
            /// Méthode requise pour la prise en charge du concepteur - ne modifiez pas
            /// le contenu de cette méthode avec l'éditeur de code.
            /// </summary>
            private void InternalStartup()
            {
                this.Startup += new System.EventHandler(ThisAddIn_Startup);
                this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
            }
            
            #endregion
        }

    this error occurs when I open an inspector for email, and I click on the button attachment. I.e

    public void OnMyButtonAttachment(Office.IRibbonControl control)
            {
                try
                {
                    if (control.Context is Outlook.Inspector)
                    {
                        ((Outlook.Inspector)control.Context).CommandBars.ExecuteMso("AttachItem");
                        
                    }
                }
                catch (System.Exception ex)
                {
                    System.Windows.Forms.MessageBox.Show(ex.Message);
                }
            }

    Thanks

    Monday, August 26, 2013 6:19 PM
  • And on what line of code does the error occur?

    If you set a breakpoint on the entry to the button click handler and step the code, make sure that each object is not null in the Locals window. Also, try changing the code to instantiate an Inspector object so you can see if the object is null. Something like this:

    public void OnMyButtonAttachment(Office.IRibbonControl control)
            {
                try
                {

                    if (control.Context is Outlook.Inspector)
                    {

                    Outlook.Inspector insp = control.context as Outlook.Inspector;

                         insp.CommandBars.ExecuteMso("AttachItem");
                        
                    }
                }
                catch (System.Exception ex)
                {
                    System.Windows.Forms.MessageBox.Show(ex.Message);
                }
            }


    Ken Slovak MVP - Outlook

    Monday, August 26, 2013 6:45 PM
  • hi! I tried to use try and catch to get where the error occurs. And I put dialog box between lines, like this:

    public void OnMyButtonAttachment(Office.IRibbonControl control)
            {
                MessageBox.Show("ok1");
                try
                {
                    MessageBox.Show("ok2");

                    if (control.Context is Outlook.Inspector)
                    {
                        MessageBox.Show("ok3");
                        Outlook.Inspector insp = control.Context as Outlook.Inspector;
                        MessageBox.Show("ok4");
                        insp.CommandBars.ExecuteMso("AttachItem");
                        MessageBox.Show("ok5");
                    }
                }
                catch (System.Exception ex)
                {
                    System.Windows.Forms.MessageBox.Show(ex.Message);
                }
            }
    all dialog box are opened except the last dialog for "ok5" and the error occurs there: hResult Efail

    Tuesday, August 27, 2013 7:29 AM
  • I'm not sure why that is failing. You do have an open Outlook mail item when you run this code?

    Try placing this little Sub into the Outlook VBA project and running it when you have an open item:

    Sub TestOpenInsertMenu()
        Dim insp As Outlook.Inspector
        Set insp = Application.ActiveInspector
        insp.CommandBars.ExecuteMso "AttachItem"
    End Sub

    I just tested this here on an Outlook 2010 machine and it worked perfectly. See if it works on your computer.


    Ken Slovak MVP - Outlook

    Wednesday, August 28, 2013 3:51 PM
  • Hi, I tried what you said but nothing occurs
    Tuesday, September 10, 2013 1:48 PM
  • Did you try stepping the code in the debugger and seeing what happens on each line?

    Don't forget, for this to work you need an open mail item, so that ActiveInspector is not Nothing.

    And just to verify, you are testing this with Outlook 2007, correct?


    Ken Slovak MVP - Outlook

    Tuesday, September 10, 2013 1:57 PM
  • Yes I did, but I saw nothing special. yes I 've an open item. I don't have outlook 2007 unfortunetely. Could you see my entire solution?
    Tuesday, September 10, 2013 2:32 PM
  • Sorry, I thought you had mentioned Outlook 2007. What Outlook version are you using?

    So if you step the VBA code and look in the Locals window you see that the ActiveInspector "insp" object is instantiated and not Nothing after the line:

    Set insp = Application.ActiveInspector

    Do you get any error message when the macro executes?


    Ken Slovak MVP - Outlook

    Tuesday, September 10, 2013 2:46 PM
  • I'm working on outlook 2010, and vs 2012, using c#. i've no error message when the macro excecutes.

    In outlook 2010, I used alt+F11 to open the window for vba macro. and I copy paste the code there:

    Sub TestOpenInsertMenu()
        Dim insp As Outlook.Inspector
        Set insp = Application.ActiveInspector
        insp.CommandBars.ExecuteMso "AttachItem"
    End Sub

    When I run itm nothing occurs, no error messages, no dialog opened. May I forgot steps?

    Tuesday, September 10, 2013 3:00 PM
  • Did you step the code as I asked and review the Locals window?

    Put a breakpoint on the line that sets the insp object variable. Open the Locals window and when your code hits the breakpoint press F8 to step the code 1 line. At that point is insp Nothing or does it have a value?

    The tags are supposed to be the same regardless of language, as far as I know. But to cover all the bases, are you running in English? If not, let's check and see if the tag is correct.

    Right-click somewhere in the mailitem Inspector ribbon and select Customize the Ribbon. In the Customize dialog select All Commands in the drop-down at the top left of the left-hand pane. Scroll down until you see the Attach Item entry. Hover over the entry and what is shown in parenthesis is the name to use. From that test here now it's showing as "AttachItemCombo", so you can try that name in the code and see if it does anything.

    The name "AttachItem" did work here, but let's try everything we can.


    Ken Slovak MVP - Outlook

    Tuesday, September 10, 2013 6:59 PM
  • Yes I stepped the code as you asked. When I put the breakpoint on the line that sets the insp object variable, in my local windwo I've "null". I supposed that the error begins there because here because the insp is empty. yes I'm running in English now.When right-click on the Customize dialog, I don't have Attach Item entry, just "Attach file" :( did you have an idea for fixing this problem Thanks
    Tuesday, September 17, 2013 8:48 AM
  • Sorry, may be I don't explain clearly what I want to do. Is it possible to attach an existing mail to another existing mail using attachItem?

    Otherwise, how could I do that?

    Tuesday, September 17, 2013 9:01 AM
  • I finally understand what you said, I run the macro on vb and it open the insert item dialog only when I open inspector for a new email, not for existing email.

    is it possible to do so?

    Tuesday, September 17, 2013 12:15 PM
  • Using that control isn't supported for an already existing "read" email. You can only use the ExecuteMso() method on controls that exist and are enabled in that context. The control you want isn't there in the context of a read email.

    Now that it's clear what you want, you'd have to create the dialog you want on your own and not use the existing Outlook dialog.


    Ken Slovak MVP - Outlook

    Tuesday, September 17, 2013 2:26 PM
  • Thnaks for information. I'm sure it will be a nightmare :( Do you know how to create dialog? is it the same thing as form region? is it possible to add a adjoining region form to select folder dialog opened by Namespace.PickFolder?
    Wednesday, September 18, 2013 12:18 PM
  • What I was talking about is a dialog created from a Windows Form or something like that. You'd have to design it from scratch to replicate as much as you can the existing look and feel of the Outlook dialog.

    You'd have to create a new ribbon button for the context you want and have the click of that ribbon button open your form (dialog) and then you'd get the results and do whatever you want with them.


    Ken Slovak MVP - Outlook

    Wednesday, September 18, 2013 3:01 PM
  • ok thanks a lot
    Thursday, September 19, 2013 9:20 AM
  • Do you know how to display folder mailitem in a form Region?
    Thursday, September 26, 2013 11:16 AM
  • What do you mean by "folder mail item"? Please explain.


    Ken Slovak MVP - Outlook

    Thursday, September 26, 2013 1:55 PM