Handle Outlook send event - vb.net

I am trying to create an Outlook add-in using VB.NET
When a user clicks on the send button in a meeting invite, I want to handle the send event, to read fields and do application specific persisting.
I am able to capture a mail send event, but the code is not fired when I modify it for a meeting send event.
I am using Visual Studio 2010 and Outlook 2010.

Simple add this function in class "ThisAddIn" and before the message send, this function will execute.
Private Sub Application_ItemSend(ByVal Item As Object, ByRef Cancel As Boolean) Handles Application.ItemSend
System.Windows.Forms.MessageBox.Show("Hello from ThisAddIn!")
End Sub

Related

Slow opening of any email after set value for an object on Application_ItemLoad

Basically I am using below code to run macro after I manually open a specific email.
The code and macro works ,but I noticed there is a slowness while opening of any email on outlook.
After many tests, I find out this line is the cause of issue:
Set MyItem = Item
And this the full code:
Option Explicit
Option Compare Text
Public WithEvents MyItem As Outlook.MailItem
Private Sub Application_ItemLoad(ByVal Item As Object)
If Item.Class = olMail Then
Set MyItem = Item 'This line cause slow opening of any email
End If
End Sub
Private Sub myItem_Open(Cancel As Boolean)
If MyItem.Subject = "Test Email" Then
'Code
End If
End Sub
Kindly How to fix this issue?
Basically I am using below code to run macro after I manually open a specific email.
You may consider using other event handlers in Outlook - for selecting in the Explorer window you may try to handle the SelectionChange event of the Explorer class which is fired when the user selects a different or additional Microsoft Outlook item programmatically or by interacting with the user interface. For opening in the separate window you can handle the NewInspector event which is fired whenever a new inspector window is opened, either as a result of user action or through program code. The event occurs after the new Inspector object is created but before the inspector window appears. Also you may handle the Inspector.Activate event which is fired when an inspector becomes the active window, either as a result of user action or through program code.
If there is a specific reason to use the ItemLoad event handler in the code you may continue using the late-binding technology and keep the item defined as object.
Also don't forget to release corresponding objects in the Unload event of the Item-level events.

Modifying default behavior of outlook buttons

Is it possible to modify the behavior of existing buttons in outlook (or generally in ms office programs)? E.g. can I make the "send mail" button show a dialog before sending a mail?
I know that you can make add ins and put them into ribbon but can you add certain behavior to existing controls?
Use Microsoft.Office.Interop.Outlook und handle the Send event. You can find the documentation here.
You have use your application reference to get the active inspector and by the active inspector you retrieve the message class:
(CType(inspector.CurrentItem, Outlook.ItemEvents_10_Event)).Send += New Outlook.ItemEvents_10_SendEventHandler(Inspector_Send)
Private Sub Inspector_Send(ByRef Cancel As Boolean)
... your code...
End Sub

Execute Outlook VBA when sending email via MAPI from another software

I use the following VBA code in ThisOulookSession to add BCC address when sending emails :
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
Dim objMe As Recipient
Set objMe = Item.Recipients.Add("some#address.dot")
objMe.Type = olBCC
objMe.Resolve
Set objMe = Nothing
End Sub
It works perfectly when sending from Outlook itself. But when I send emails via my CRM program using MAPI, the VBA is not executed. So I think sending emails using MAPI does not take ThisOutlookSession in account.
Is there a way to execute my VBA macro both with Outlook itself and via MAPI ?
Application.ItemSend event does not fire when a message is sent through Simple MAPI or a mailto link. This is by design.

Automatically save draft email

I'm looking for VBA code for Outlook 2013 that will automatically save a draft either when opening the composition window, when replying or forwarding an email (it's OK if it fires on newly-composed messages also) or just before sending it when hitting the Send button.
Essentially emulating hitting the "Save" icon or Ctrl-S either before composing or before the message gets sent.
Outlook generates a "Could not complete the operation because the mail provider does not support it" error when replying to or forwarding a non-Google Apps account email when Google Apps accounts are combined with other email accounts in Outlook.
I discovered that a work-around is to manually save the email before sending it. I can do that manually but often forget. Can't do it after the error message appears. Trying to save after the "could not complete" error is thrown results in another error.
BTW, I already have a routine running off the Application_ItemSend event; will adding another conflict?
Update: Here is a version of what I tried:
Private Sub Application_ItemSend(ByVal item As Object, Cancel As Boolean)
' Runs when the item is being sent.
Call AutoSaveDraft(item)
Call CheckSubject(item)
End Sub
(CheckSubject fires)
Private Sub AutoSaveDraft(item As Object)
item.Save
SendKeys "+{F12}" ' Save the message. ##
SendKeys "^S"
Sleep 1000 ' Pause execution 1 second. The Windows sleep function is called in the beginning ##
End Sub
(Yes, I have all three potential save methods. Have also tried them individually.
You are free to call the Save method in the ItemSend event handler if it helps to avoid issues.
You may find the Getting Started with VBA in Outlook 2010 article helpful.
Since the original poster doesn't make it clear, this is what actually works to prevent the errors related to non-Google Apps accounts when simply hitting Send:
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
Item.Save
End Sub

Finding Outlook event triggered by third party email send

I have a third party piece of software that sends email through Outlook. It allows us to specify a "reply to" email address, but not a "from" email address. I'm trying to write something in VBA that notices when a message comes from that third party software and uses the "reply to" address as the "from" address before it sends the email. I'm having trouble getting any events to trigger when I send email with the third party software.
If I use
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
MsgBox "Test"
End Sub
it triggers the message box when I send an email through Outlook, but not when the third party software does. Is there another event I should be looking at instead? I'm looking at Application events or MailItem events (http://msdn.microsoft.com/en-us/library/office/dn320237(v=office.15).aspx) and nothing but the Send events seem at all appropriate. I tried the sample code for the item Send event
Public WithEvents myItem As Outlook.MailItem
Sub SendMyMail()
Set myItem = Outlook.CreateItem(olMailItem)
myItem.To = "Dan Wilson"
myItem.Subject = "Data files information"
myItem.Send
End Sub
Private Sub myItem_Send(Cancel As Boolean)
myItem.ExpiryTime = #2/2/2003 4:00:00 PM#
End Sub
but it doesn't trigger even when I send an email through Outlook. I did try restarting Outlook before testing that out.
This question looks promising: How do I trigger a macro to run after a new mail is received in Outlook?
but it involves setting an event listener to the Inbox. I've heard that there's a way to change settings somehow in a way that prevents the third party messages from being automatically sent, and when that happens they get stuck in Outlook's Drafts folder. How would I go about putting an event listener on the Drafts folder instead of the Inbox?
Application.ItemSend event is only fired if a message is sent through the Outlook Object Model. If the other app uses Extended MAPI, no event fires.