Reputation: 2731
In a C# VSTO Outlook addin I am moving a MailItem to a folder.
I have tried the following code which already has a reference to the destination folder (olFld) and to a valid id (entryId) string.
olNS = olFld.Session as Outlook.NameSpace;
//get the new mailitem
olMailItem = olNS.GetItemFromID(entryId, olFld.StoreID) as Outlook.MailItem;
//move it to the right folder.
selMailItem = olMailItem.Move(olFld) as Outlook.MailItem;
olApp = selMailItem.Application as Outlook.Application;
olExp = olApp.ActiveExplorer() as Outlook.Explorer;
olExp.ClearSelection();
if (olExp.IsItemSelectableInView(selMailItem))
olExp.AddToSelection(selMailItem);
IsItemSelectableInView is returning false.
Is there a way to move a mailitem and have it selected?
EDIT UPDATE
I think it is a timing issue. I added the SelectionChange event to the explorer and clalled ClearSelection.
olNS = olFld.Session as Outlook.NameSpace;
//get the new mailitem
olMailItem = olNS.GetItemFromID(entryId, olFld.StoreID) as Outlook.MailItem;
//move it to the right folder.
selMailItem = olMailItem.Move(olFld) as Outlook.MailItem;
olApp = selMailItem.Application as Outlook.Application;
olExp = olApp.ActiveExplorer() as Outlook.Explorer;
//added this *******************
olExp.SelectionChange += OlExp_SelectionChange;
//only a clear selection here
olExp.ClearSelection();
Then in the event I have this.
if (olExp.Selection.Count != 0)
{
olExp.SelectionChange -= OlExp_SelectionChange;
olExp.ClearSelection();
if (olExp.IsItemSelectableInView(selMailItem))
olExp.AddToSelection(selMailItem);
if (selMailItem != null) ReleaseComObject(selMailItem);
if (olExp != null) ReleaseComObject(olExp);
}
From what it looks like the ClearSelection sets the selction count to 0 and then Outlook autoselects something due to the results of the move (unfortunately not the moved email) and then the above code will change the selection to my mailitem.
It looks like a hack and in code and is not clean in the UI.
UPDATE Based on Comments in below answer
This seems to give it enough time for outlook to update the UI after the move.
System.Windows.Threading.Dispatcher.CurrentDispatcher.InvokeAsync(new Action(() =>
{
olExp.ClearSelection();
if (olExp.IsItemSelectableInView(selMailItem))
olExp.AddToSelection(selMailItem);
}));
Upvotes: 1
Views: 74
Reputation: 66286
If the destination folder is not yet shown in Outlook, you need to select it first by setting the Explorer.CurrentFolder
property.
Upvotes: 0