Explorer.AddToSelection() and Explorer.RemoveToSelection() methods in Outlook 2010

Add-in Express™ Support Service
That's what is more important than anything else

Explorer.AddToSelection() and Explorer.RemoveToSelection() methods in Outlook 2010
How to use Explorer.AddToSelection() and Explorer.RemoveToSelection() methods in Outlook 2010 
Mukesh Kumar


Guest


Hello Sir,

I am using add-in-express API as premium version (6.3.3052) on Outlook 2010 with Visual Studio 2008 (SP1).

I am trying to set the email (by giving EntryID) to ReadingPane, which is only possible in 2010 as I learned with using Explorer.AddToSelection() and Explorer.RemoveToSelection() methods. But I am not able to find these method and how can I use these methods.

So can you please tell me how can I use these methods to set/clear the mail item via programmatically.


Thanks,
Mukesh Kumar
Posted 25 Dec, 2010 08:27:12 Top
Andrei Smolin


Add-in Express team


Posts: 18833
Joined: 2006-05-11
Hello Mukesh,

I suppose that you don't see those methods because you use version-neutral interops in your project; check if there is Introp.Outlook.DLL in your project references. If this is the case, please read http://www.add-in-express.com/creating-addins-blog/2010/03/16/interop-assemblies-late-binding/


Andrei Smolin
Add-in Express Team Leader
Posted 28 Dec, 2010 03:53:30 Top
Mukesh Kumar


Guest


Hello Andrei Smolin,

Thanks for reply.

I am trying with the late binding, but I am not able to select the mail (with giving EntryID) in the ReadingPane programmatically. So can you please give me a line of code which helps me to perform the task, it will be really very appreciable.


Thanks,
Mukesh Kumar
Posted 28 Dec, 2010 05:46:17 Top
Andrei Smolin


Add-in Express team


Posts: 18833
Joined: 2006-05-11
Hi Mukesh,

Below is a raw sketch of the early-binding version of that functionality:

string entryId = "";
private void adxRibbonButton1_OnClick(object sender, AddinExpress.MSO.IRibbonControl control, bool pressed)
{
    Outlook._Explorer explorer = OutlookApp.ActiveExplorer();
    Outlook.Selection selection = null;
    object item = null;
    Outlook.MailItem mail = null;
    try 
    {
        selection = explorer.Selection;
    }
    catch {}
    if (selection != null)
    {
        if (selection.Count > 0)
        {
            item = selection[1];
            mail = item as Outlook.MailItem;
            entryId = mail.EntryID;
            Marshal.ReleaseComObject(item);
        }
        Marshal.ReleaseComObject(selection);
    }
    Marshal.ReleaseComObject(explorer);
}

private void adxRibbonButton2_OnClick(object sender, AddinExpress.MSO.IRibbonControl control, bool pressed)
{
    Outlook._Explorer explorer = OutlookApp.ActiveExplorer();
    explorer.ClearSelection();
    Outlook.NameSpace ns = OutlookApp.Session;
    object item = ns.GetItemFromID(entryId, Type.Missing);
    explorer.AddToSelection(item);
    Marshal.ReleaseComObject(item);
    Marshal.ReleaseComObject(ns);
    Marshal.ReleaseComObject(explorer);
}



Andrei Smolin
Add-in Express Team Leader
Posted 28 Dec, 2010 09:15:08 Top
Mukesh Kumar


Guest


Hello Andrei Smolin,

Thanks for reply with code.

I tried the above code, but again the problem is on explorer.ClearSelection() and explorer.AddToSelection(item).

It shows as the following exception:

Error 1 'Outlook._Explorer' does not contain a definition for 'ClearSelection' and no extension method 'ClearSelection' accepting a first argument of type 'Outlook._Explorer' could be found (are you missing a using directive or an assembly reference?) E:\Users\Varinder\Documents\Visual Studio 2008\Projects\MyAddin1\MyAddin1\AddinModule.cs 206 22 MyAddin1

I am also sending you the sample application project solution, on which I am getting this issue, please have a look and suggest me where I am wrong.



Thanks,
Mukesh Kumar
Posted 29 Dec, 2010 01:23:06 Top
Andrei Smolin


Add-in Express team


Posts: 18833
Joined: 2006-05-11
Hi Mukesh,

I've transformed that sample code to use late binding:

private void adxRibbonButton2_OnClick(object sender, AddinExpress.MSO.IRibbonControl control, bool pressed)
{
    Outlook._Explorer explorer = OutlookApp.ActiveExplorer();
    //explorer.ClearSelection();
    explorer.GetType().InvokeMember("ClearSelection", System.Reflection.BindingFlags.InvokeMethod, null, explorer, null);
    Outlook.NameSpace ns = OutlookApp.Session;
    object item = ns.GetItemFromID(entryId, Type.Missing);
    //explorer.AddToSelection(item);
    explorer.GetType().InvokeMember("AddToSelection", System.Reflection.BindingFlags.InvokeMethod, null, explorer, new object[] { item });
    Marshal.ReleaseComObject(item);
    Marshal.ReleaseComObject(ns);
    Marshal.ReleaseComObject(explorer);
}


Doe this work for you?


Andrei Smolin
Add-in Express Team Leader
Posted 29 Dec, 2010 07:09:22 Top
Mukesh Kumar


Guest


Hello Andrei Smolin,

I really very appreciate your help, your suggested solution is working perfectly on my end.


Thanks,
Mukesh Kumar
Posted 29 Dec, 2010 11:00:40 Top
Andrei Smolin


Add-in Express team


Posts: 18833
Joined: 2006-05-11
You are welcome, Mukesh!


Andrei Smolin
Add-in Express Team Leader
Posted 29 Dec, 2010 11:28:30 Top
Mukesh Kumar


Guest


Hello Andrei Smolin,

I am facing one small problem with the suggested solution as below:

Suppose I get the mail's entryid with using first button (adxRibbonButton1) from the Inbox folder and I changed the folder from Inbox to Draft and then I am trying the second button (adxRibbonButton2), then it shows the following exception:

"Exception has been thrown by the target of an invocation."

Please try this on your end and suggest me how can we tackle this issue.



Thanks,
Mukesh Kumar
Posted 30 Dec, 2010 02:13:38 Top
Eugene Astafiev


Guest


Hi Mukesh,

It looks like the following line of code fires an exception:

    explorer.GetType().InvokeMember("AddToSelection", System.Reflection.BindingFlags.InvokeMethod, null, explorer, new object[] { item }); 


Am I right? If so, note that you can't add an item to the selection from another folder. Please use try/catch block in that case. Does it help?
Posted 03 Jan, 2011 09:25:03 Top