VSTO Outlook: Catching Send Event - c#

I am trying to create plug-in that will catch any send item and show warning before sent out.
Currently I am using the below code
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
Outlook.Application application = Globals.ThisAddIn.Application;
application.ItemSend += new Outlook.ApplicationEvents_11_ItemSendEventHandler(Application_ItemSend);
}
However, I noticed it catches almost all the send event but when send button is pressed from main screen of Outlook, I mean when the mail is not popped out to separate window, it does not catch the event and my plugin function is not executed. My environment is Visual Studio 2019 and Outlook 2019. Any advise is appreciated.
Thank you,
Catch all the send event from Outlook.
Update 11/7/2022: it turned out that the event is caught but
I am tryning to catch the item type by the below method and inline item does not detected as an item.
itemtype = Application.ActiveWindow().CurrentItem.Class;

I can confirm that the ItemSend event is fired for all outgoing emails in Outlook, including inline responses. To make sure I've just created a sample VSTO add-in and placed your code there. Everything works correctly when I reply to items in-place. Try to declare the Application object at the class level. But I think everything should work as is now.
Outlook.Application application = null;
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
application = Globals.ThisAddIn.Application;
application.ItemSend += new Outlook.ApplicationEvents_11_ItemSendEventHandler(Application_ItemSend);
}
You may consider subscribing to the Explorer.InlineResponse event which is fired when the user performs an action that causes an inline response to appear in the Reading Pane. So, you may get the item composed and do whatever you need (subscribe to the Send event).
Be aware, a low level API such as Extended or Simple MAPI doesn't trigger the ItemSend event in Outlook - only actions made in the Outlook object model or made manually can trigger the event.

It should work in all cases, but Outlook disables most OOM events for the items created using Simple MAPI or from the "mailto:" links. Is that the case?

Related

VSTO my add in is prevent other Outlook items

I am adding a plugin in Outlook (2013 and up). Click on My Add-on Open a specific parameters window, this window becomes modal and prevents access to other Outlook items.
Only if I confirm or cancel this parameters can I access other items.
I probably need to add some functionality in Click_event to the ribbon in my-add-in.
private void Button_Click(object sender, RibbonControlEventArgs e){
//e.Control.Tag is "RibbonId=Microsoft.Outlook.Explorer"
}
Any idea how can I prevent this behavior? What I need add to my click_event ?
It seems you need to show a non-modal window by using the Form.Show(IWin32Window) method instead of Form.ShowDialog.

C# Outlook Remove event when email sent successfully?

I have a plugin outlook, I create an event when an email sent successfully, this code:
private Outlook.Items _items;
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
Outlook.Application application = this.Application;
_items = Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail).Items;
_items.ItemAdd += new Outlook.ItemsEvents_ItemAddEventHandler(Items_ItemAdd);
}
Then, I have the user setting, and if the user doesn't select, I want to remove that event (Items_ItemAdd).
So should I do?
Well, tracking the Sent Items folder is not really a good idea. Outlook allows to remove a sent item by skipping the Sent Items folder. The DeleteAfterSubmit property of Outlook items is a Boolean value that is True if a copy of the mail message is not saved upon being sent, and False if a copy is saved in Sent Items folder. So, you will never get the event fired if users or other software like VBA macros or add-ins set this property before sending emails.
A better way is to handle the ItemSend event of the Application which is fired when a new item is received in the Inbox.
This event fires once for every received item that is processed by Microsoft Outlook. The item can be one of several different item types, for example, MailItem , MeetingItem , or SharingItem. The EntryIDsCollection string contains the Entry ID that corresponds to that item. Note that this behavior has changed from earlier versions of the event when the EntryIDCollection contained a list of comma-delimited Entry IDs of all the items received in the Inbox since the last time the event was fired.
The NewMailEx event fires when a new message arrives in the Inbox and before client rule processing occurs. You can use the Entry ID returned in the EntryIDCollection array to call the NameSpace.GetItemFromID method and process the item. Use this method with caution to minimize the impact on Outlook performance. However, depending on the setup on the client computer, after a new message arrives in the Inbox, processes like spam filtering and client rules that move the new message from the Inbox to another folder can occur asynchronously. You should not assume that after these events fire, you will always get a one-item increase in the number of items in the Inbox.
In the NewMailEx event handler you may ask users whether to process emails after sending and, if it is not, you can simply set the DeleteAfterSubmit property to true.

Outlook AddIn: showing popup while dragging mail in Outlook

I have an outlook addin which shows a popup after sending mail which asks the user whether he/she wants to save the mail.
The problem is that this popup is shown when a mail is added to the sent folder.
So when you send a mail and immediately start dragging a mail to another folder because, e.g. you want to clean up mails, then the popup shows when you're in the middle of dragging which stops the dragging and makes the popup with the question unresponsive to the mouse.
Is there any way around this?
Thanks in advance!
The event for an mail being added to sent folder is set in the Ribbon Load method:
private void CodexRibbon_Load(object sender, RibbonUIEventArgs e)
{
if (!Globals.ThisAddIn.FirstLoadComplete)
{
Messenger.Default.Register<object>(this, ItemSend);
Globals.ThisAddIn.FirstLoadComplete = true;
}
}
The question is asked as follows:
private void ItemSend(object item)
{
if (MessageBox.Show(#" Wilt u de zojuist verzonden e-mail opslaan?", #"Opslaan in Codex", MessageBoxButtons.YesNo) == DialogResult.Yes)
ShowSaveWindow(item);
}
I don't think there is a way to find out if the control is inside the DoDragDrop Windows API function (you can patch it and have the patched version set some kind of a global flag before entering the original function, but I don't think that will be a good idea in .Net).
Try to display the message box in the Application.ItemSendevent handler, mark the message somehow (user property?), then do the processing in Items.ItemAdd without a message box.

mailItem.PropertyChange stops firing

I am implementing Custom Task Panes with E-Mail Messages in Outlook.
The core is taken from this link MSDN (Walkthrough: Displaying Custom Task Panes with E-Mail Messages in Outlook)
Handler for property change is added:
void TaskPane_VisibleChanged(object sender, EventArgs e)
{
Globals.Ribbons[inspector].ManageTaskPaneRibbon
.toggleButton1.Checked = taskPane.Visible;
...some code here...
mailItem.PropertyChange += PropertyChangeHandler;
}
PropertyChangeHandler checks is recipients have changed and does some heavy routine with posts and so on. But... If I add 10 recipients and start to remove them with backspace PropertyChangeHandler stops firing at some point.
No errors. Buttons on custom task pane work fine.
What is wrong?
Seems that either event is eaten or inspector is incorrect, but I cannot spot problem and find the solution.
I also think that it might be about "heavy load" when next event is fired before previous is completed, but this is a guess
You need to call the Save method or save the message explicitly to make the PropertyChange event fired. Outlook caches values in the UI and doesn't propagate changes until the item is saved.
Also I'd suggest creating a log file (a regular text file) where you can write the debug statements. Thus, you will understand what happens in the code.

How to focus an Outlook 2010 custom ribbon tab

I have a custom Outlook 2010 Ribbon tab that has the type Microsoft.Outlook.Appointment.
In that tab, I have several buttons that change the current appointment item, and call its Save method. However, that method always changes the focus to the first tab of the inspector. I want the focus to remain on my custom tab.
Here is my current code:
private void ButtonSaveAppointment(object sender, RibbonControlEventArgs e)
{
Outlook.Inspector inspector = (Outlook.Inspector)this.Context;
Outlook.AppointmentItem appointment = (Outlook.AppointmentItem)inspector.CurrentItem;
appointment.Save();
this.RibbonUI.ActivateTab(this.Tabs[0].ControlId.ToString());
}
This does not work for me. Can anyone tell me what am i doing wrong?
Thank you.
This is what i use in my load event:
ThisRibbonCollection ribbonCollection = Globals.Ribbons[_inspector];
ribbonCollection.RibbonSMS.RibbonUI.ActivateTab("the_name_of_the_ribbon_tab");
And that would correspond to what i see in your code:
this.RibbonUI.ActivateTab(this.Tabs[0].ControlId.ToString());
That code fires well in the load event, but i tested it and it does not work when you are calling it from a function. Please try and move it to the load event and you will see that the code works. My guess is that it does not work properly from a method but just form the load event.
Good luck

Categories