none
Application_ItemSend not accessed when sending email outside outlook (i.e. word or send to mail from desktop) RRS feed

  • Question

  • I have a working piece of code in the ThisOutlookSession that traps when the user is sending an email, except if the mail is sent from Word or Excel by using the Save&Send (as attachment or pdf) option.
    I can't find any hint anywhere of how to make the code work also when doing it this way. 
    The mail is sent, but ItemSend event isn't fired can you please provide some help?

    Code

    Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
     Dim strSubject As String
      strSubject = Item.Subject
         If Len(Trim(strSubject)) = 0 Then
             prompt$ = "Subject is Empty. Are you sure you want to send the Mail?"
           If MsgBox(prompt$, vbYesNo + vbQuestion + vbMsgBoxSetForeground, "Check for Subject") = vbNo Then
            Cancel = True
          End If
        End If
    End Sub


    Wednesday, May 28, 2014 8:18 PM

Answers

  • Outlook macros won't run if Simple MAPI is used to create and open the mail item. When Word, Excel, Windows Explorer and other apps use their Send methods they use Simple MAPI. That opens the email as a modal email window (Inspector) using plain text.

    The way this can get handled is with an Outlook COM addin. When the Simple MAPI window is opened it creates an Inspector and adds it to the Inspectors collection. However, the NewInspector() event does not fire.

    To handle this you need to run a timer in the same thread as the Outlook application. When the timer fires you can check the Inspectors collection for Inspectors that you aren't handling. If you find a Simple MAPI Inspector you can add handling for it and add event handlers such as the Send() event handler.

    That will let you handle those Inspectors. You just have to make sure the code runs in the Outlook thread so as not to crash or hang Outlook when the object model is used.


    Ken Slovak MVP - Outlook

    Thursday, May 29, 2014 2:26 PM
    Moderator

All replies

  • This is the forum to discuss questions and feedback for Microsoft Office client, better to post your question to the forum of Outlook for Developers:

    http://social.msdn.microsoft.com/Forums/office/en-US/home?forum=outlookdev

    The reason why we recommend posting appropriately is you will get the most qualified pool of respondents, and other partners who read the forums regularly can either share their knowledge or learn from your interaction with us. Thank you for your understanding.

    Thanks,

    Ethan Hua CHN
    TechNet Community Support

    Thursday, May 29, 2014 2:40 AM
  • Hello Anastasios,

    You may find the Outlook 2003 itemsend not firing forum thread helpful. Also see ItemSend not firing for outlook 2010 - Security is Low .

    The similar issue was described in the Outlook 2010 - On mail send item - macro doesn't run forum thread. Did you have a chance to check out the Trust Center settings in Outlook?

    Thursday, May 29, 2014 6:16 AM
  • Hi,

    I'm using outlook 2007.  The itemSend is firing perfectly when the email is created by outlook->new.

    The problem is when an email is send form MS Word or other programs.

    Thanks in advance for your help!

    Thursday, May 29, 2014 8:35 AM
  • Hi

    Which feature in Word do you use to send emails from Outlook?

    After sending the email, can you find it in "sent items" folder?


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Thursday, May 29, 2014 9:56 AM
    Moderator
  • Hi,

    when in MS Word and try to send a doc from within word (Office Button -> Send -> Email), then itemSend (from ThisOutlookSession) seems to be bypassed. This happens in both cases Outlook opened or Outlook closed.

    After sending the email, I find the email in the "Sent" folder.

    Anastasios



    Thursday, May 29, 2014 10:16 AM
  • Did you have a chance to check out the list of running processes when you do so in Word?

    Most probably Outlook is not used for sending mails in that case, a low level API is used instead. Could you please confirm? 

    Thursday, May 29, 2014 1:38 PM
  • Outlook macros won't run if Simple MAPI is used to create and open the mail item. When Word, Excel, Windows Explorer and other apps use their Send methods they use Simple MAPI. That opens the email as a modal email window (Inspector) using plain text.

    The way this can get handled is with an Outlook COM addin. When the Simple MAPI window is opened it creates an Inspector and adds it to the Inspectors collection. However, the NewInspector() event does not fire.

    To handle this you need to run a timer in the same thread as the Outlook application. When the timer fires you can check the Inspectors collection for Inspectors that you aren't handling. If you find a Simple MAPI Inspector you can add handling for it and add event handlers such as the Send() event handler.

    That will let you handle those Inspectors. You just have to make sure the code runs in the Outlook thread so as not to crash or hang Outlook when the object model is used.


    Ken Slovak MVP - Outlook

    Thursday, May 29, 2014 2:26 PM
    Moderator
  • Thank you very much for your prompt response and you help!!!

    I see it is far more complicated than I initial thought and my VBA knowledge does not allow me to implement it. Do you not if I can find any code sample for it anywhere?

    Thanks again ,

    Anastasios

    Friday, May 30, 2014 8:15 PM
  • There are a number of examples of VB6 timers, many of those are compatible with VBA. A place to look would be www.vbaccelerator.com, in the VB section. For example, http://www.vbaccelerator.com/home/VB/Code/Libraries/Subclassing_and_Hooking_with_Machine_Code_Thunks/article.asp.

    I haven't studied the code, so I don't know how applicable to VBA it really is. Also, if you do create code to handle Simple MAPI Inspectors understand that it's not supported by MS, so you're sailing in uncharted waters.

    I also did an Internet search using "handle outlook simple mapi inspectors" and came up with a number of relevant threads. It seems I've been answering questions about this for a very long time :)


    Ken Slovak MVP - Outlook

    Friday, May 30, 2014 8:38 PM
    Moderator
  • Thanks again for your reply. One last question does my code above need any error checks to be included?
    Tuesday, June 3, 2014 10:11 PM
  • The code listed in your initial message there is very simple. So, I don't see any week places for checking errors.
    Wednesday, June 4, 2014 12:34 PM
  • All Outlook code, especially if it's managed code or COM addin code, should include error handling of some sort. Even if it's just a simple try...catch block or for VBA an On Error GoTo label:.

    Ken Slovak MVP - Outlook

    Wednesday, June 4, 2014 5:48 PM
    Moderator
  • It is definitely not a good practice to wrap all the code in the try/catch blocks.
    Wednesday, June 4, 2014 6:18 PM
  • And exactly why do you think it's not a good practice to handle any possible exceptions?

    Especially when all possible Outlook errors must be handled to prevent the code from becoming disabled if there is an exception?


    Ken Slovak MVP - Outlook

    Wednesday, June 4, 2014 7:09 PM
    Moderator
  • I'd recommend mixing the defensive programming style with exception handling due to the fact that try/catch blocks slows down the performance.
    Wednesday, June 4, 2014 8:11 PM
  •  

    So, what kind of error handling do you propose?

    Is it ok if I put "On Error Resume Next" at the begining or this is bad practice as it does not fix the errors but just ignore them?

    Thursday, June 5, 2014 5:59 AM
  • I am not a VB developer, but you need to prevent any exceptions from your add-in because Outlook can disable it. I don't think that your code in the ItemSend event will throw any exceptions - it is very simple. However, It is up to you how to not let exceptions throw away. Also I'd recommend using any logging mechanism. Thus, you will be able to find a problematic property or method call and fix it quickly (or implement a workaround - sometimes things are not under our control).
    Thursday, June 5, 2014 11:48 AM
  • As this is VBA code running in ThisOutlookSession I wouldn't get too fancy. On Error Resume Next is pretty minimal, and as you mentioned just tries to ignore any exceptions. I'd probably use an error handler.

    Logging isn't usually used with VBA code, although it could be.

    I'd probably use something like this:

    Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
     Dim strSubject As String

     On Error GoTo Fubar

    ' other code

        EndIf

    EndIf

    GoTo Exit

    Fubar:

    MsgBox "Error # : " & Err.Number & vbCRLF & Err.Description

    Exit:

    End Sub


    Ken Slovak MVP - Outlook

    Thursday, June 5, 2014 2:21 PM
    Moderator