none
MailItem Events RRS feed

  • Question

  • Hi,

    Is there an event I can intercept for Forward As Attachment. All the other ones exist:

    Item_Close
    Item_Open
    Item_Reply
    Item_ReplyAll
    Item_Forward

    Item_ForwardAsAttachment doesn't do anything.

    Thanks

    Private Sub Item_Forward(ByVal Response As Object, Cancel As Boolean)
    
        Response.SendUsingAccount = DefaultAccount()
        
    End Sub
    
    Private Sub Item_ForwardAsAttachment(ByVal Response As Object, Cancel As Boolean)
    
        Response.SendUsingAccount = DefaultAccount()
        
    End Sub
    

    • Moved by Perry-Pan Friday, January 25, 2019 1:56 AM
    Thursday, January 24, 2019 3:52 PM

All replies

  • My fellow Outlook MVPs Eric and Dmitry have posted a good answer to that here;
    How to detect other outlook events such as “Forward as attachment”, “Reply with Meeting”



    Robert Sparnaaij [MVP-Outlook]
    Outlook guides and more: HowTo-Outlook.com
    Outlook Quick Tips: MSOutlook.info

    Thursday, January 24, 2019 8:18 PM
  • Thanks Roady.

    I should have said this is for VBA. I tried those values but it doesn't pick them up.

    Thursday, January 24, 2019 10:47 PM
  • Hi Lanky Doodle,

    Thank you for visiting Outlook IT forum.

    As here we mainly focus on issues regarding Outlook desktop client, I’m not familiar with VBA code. I’ll help move this thread to Outlook for Developer forum to see if anyone else has some good ideas.

    Thank you for your understanding and support.

    Regards,

    Perry


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnsf@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    Friday, January 25, 2019 1:56 AM
  • You are out of luck in case of VBA I am afraid - VBA won't let you do raw COM events the way C++, Delphi, or even C# can do. The event must be exposed through the type library for VBA to use it.

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Friday, January 25, 2019 2:20 AM
  • Thanks for the update.

    I don't know if I just got lucky but I think I found a way. Basically rather than hook in to each function (Reply, Reply All etc.), hook into ANY MailItem inspector that has .Sent = False when it is activated. This includes composing new messages too.

    Option Explicit
    
    Public WithEvents Inspectors As Outlook.Inspectors
    Public WithEvents Inspector As Outlook.Inspector
    Public WithEvents Item As Outlook.MailItem
    
    Private Sub Application_Startup()
    
        Set Inspectors = Application.Inspectors
        
    End Sub
    
    Private Sub Inspectors_NewInspector(ByVal NewInspector As Outlook.Inspector)
    
        Set Inspector = NewInspector
        
    End Sub
    
    Private Sub Inspector_Activate()
    
        If Inspector.CurrentItem.Class = olMail Then
        
            Set Item = Inspector.CurrentItem
            If Item.Sent = False Then Item.SendUsingAccount = DefaultMailAccount()
            
        End If
        
    End Sub
    
    Private Sub Item_Close(Cancel As Boolean)
    
        Set Item = Nothing
        Set Inspector = Nothing
        
    End Sub

    Probably could be refined. Would like to include Reply with Meeting, so appointment item.




    Friday, January 25, 2019 2:45 PM
  • That will work as long as you don't need access to the original item and don't care that you can't cancel the event. Or until Outlook starts displaying the item in the inline editor :-)

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Friday, January 25, 2019 3:00 PM
  • Weirdly this all works fine on my machine which is Windows 10/Office 2016.

    Copied the code to another user who is also on Windows 10/Office 2016 and it does nothing. I Debug.Print in relevant places and it's hitting the right parts of the code, just not changing the SendUsingAccount value.

    On my machine, it changes it before the inspector is shown so the From box is filled with the account I want. On the other machine it refuses to do this and just shows the mailbox the email is in in the From box.

    Works perfectly on a Windows 7/Office 2010 machine too!

    Hate Microsoft sometimes. Either make something work or don't make it work!


    Friday, January 25, 2019 5:06 PM
  • That event was probably added at a request from one of the ISVs who could use it without explicitly supporting it in the type library (which would require extra work to test and document it). I'd rather take that than no event at all :-)

    You might want to post your problematic code.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Friday, January 25, 2019 5:15 PM
  • Ok. If the additional account is one word, so Hello / hello@domain.com it seems to work fine.

    If the additional account is not one word, so Hello Goodday / hello.goodday@domain.com it doesn't work.

    What's up with that?


    Friday, January 25, 2019 5:16 PM
  • Please show your code.

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Friday, January 25, 2019 5:18 PM
  • ThisOutlookSession:

    Option Explicit
    
    Public WithEvents Inspectors As Outlook.Inspectors
    Public WithEvents Inspector As Outlook.Inspector
    Public WithEvents MailItem As Outlook.MailItem
    
    Private Sub Application_Startup()
    
        Set Inspectors = Application.Inspectors
        
    End Sub
    
    Private Sub Inspectors_NewInspector(ByVal NewInspector As Outlook.Inspector)
    
        Set Inspector = NewInspector
        
    End Sub
    
    Private Sub Inspector_Activate()
    
        If Inspector.CurrentItem.Class = olMail Then
        
            Set MailItem = Inspector.CurrentItem
            If Not MailItem.Sent Then MailItem.SendUsingAccount = DefaultMailAccount()
            
        End If
        
    End Sub
    
    Private Sub MailItem_Close(Cancel As Boolean)
    
        On Error Resume Next
        If Not MailItem Is Nothing Then Set MailItem = Nothing
        If Not Inspector Is Nothing Then Set Inspector = Nothing
        
    End Sub
    
    

    Module1:

    Function DefaultMailAccount() As Outlook.Account
    
        Set DefaultMailAccount = Application.Session.Accounts(1)
        Exit Function
        
    End Function
    
    

    This works fine on my computer (Win10/Off16). My own account is firstname.lastname@domain.com and additional account is singleword@domain.com

    This same code works fine in Win7/Off10 for an additional account that is also just singleword@domain.com. If I add an additional account that is oneword.twowords@domain.com it fails.

    That's exactly the same for the other Win10/Off16 computer. I added a singleword@domain.com account and it works fine.

    Friday, January 25, 2019 5:37 PM
  • Are you sure DefaultMailAccount returns the right object?

    Keep in mind that Inspector_Activate will fire more than once. Also keep in mind that you only support a single open inspector. A user can have more than one inspector.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Friday, January 25, 2019 5:45 PM
  • Yes, 100%. If I debug.print DefaultMailAccount it always returns the right account - the default account as set in Outlook.

    Yes I realize that too. I don't ever need to keep track of each inspector. All that matters is that each new, reply etc. opens with the DefaultMailAccount value. If they compose 1000 messages, that's fine.

    Friday, January 25, 2019 8:01 PM
  • Did you do Debug.Print MailItem.SendUsingAccount?

    If you care about all replies, you would also need to track the Explorer.InlineResponse event and get rid of the item level event handlers.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Friday, January 25, 2019 8:08 PM
  • Just did a quick test.

    Private Sub Inspector_Activate()
    
        Debug.Print "Before setting: " & Inspector.CurrentItem.SendUsingAccount
        Debug.Print "Default account: " & DefaultMailAccount()
        
        If Inspector.CurrentItem.Class = olMail Then
        
            If Not Inspector.CurrentItem.Sent Then Inspector.CurrentItem.SendUsingAccount = DefaultMailAccount()
            
        End If
        
        Debug.Print "After setting: " & Inspector.CurrentItem.SendUsingAccount
        
    End Sub

    Results:

    From email in users default account:

    Before setting: Firstname.Lastname@domain.com
    Default account: Firstname.Lastname@domain.com
    After setting: Firstname.Lastname@domain.com

    From email in single word mailbox:

    Before setting: SharedMailbox1@domain.com
    Default account: Firstname.Lastname@domain.com
    After setting: Firstname.Lastname@domain.com

    From email in multi word mailbox:

    Before setting: Shared.Mailbox2@domain.com
    Default account: Firstname.Lastname@domain.com
    After setting: Firstname.Lastname@domain.com

    So, the code actually seems to be working but it is NOT changing it in the inspector for the multi word mailbox.


    Friday, January 25, 2019 8:22 PM
  • OK. When I hit New, it works. If I hit Reply, Reply All, Forward or Forward as Attachment it doesn't.

    The difference is with New, the from box displays accountname@domain.com, all the other actions show just accountname (without the domain suffix). This means the from box is not quite right, and if I click the from drop down box and change it to accountname@domain.com, then manually run this:

    Public Sub test()
    
        Application.ActiveInspector.CurrentItem.SendUsingAccount = Application.Session.Accounts(1)
    
    End Sub
    

    it then works as it changes it to firstname.lastname@domain.com

    This must surely be a bug. Why would New display as accountname@domain.com but all the other as just accoutname?

    I even started again from this: http://www.vboffice.net/en/developers/newinspector-and-inspector-activate

    So now have this:

    Private WithEvents m_Inspectors As Outlook.Inspectors
    Private WithEvents m_Inspector As Outlook.Inspector
    
    Private Sub Application_Startup()
      Set m_Inspectors = Application.Inspectors
    End Sub
    
    Private Sub m_Inspectors_NewInspector(ByVal Inspector As Outlook.Inspector)
      If TypeOf Inspector.CurrentItem Is Outlook.MailItem Then
        'Handle emails only
        Set m_Inspector = Inspector
      End If
    End Sub
    
    Private Sub m_Inspector_Activate()
      
      Dim Mail As Outlook.MailItem
      Set Mail = m_Inspector.CurrentItem
      If Mail.Sent = False Then
        'Edit the subject only for new emails
        Mail.SendUsingAccount = Application.Session.Accounts(1)
        Mail.Subject = "test"
      End If
      Set m_Inspector = Nothing
    End Sub
    
    Public Sub test()
    
        Application.ActiveInspector.CurrentItem.SendUsingAccount = Application.Session.Accounts(1)
    
    End Sub
    

    Saturday, January 26, 2019 9:39 AM
  • I should also have said that I am referring to Exchange accounts, not pop/imap.

    I'm reading that SendUsingAccount doesn't work with Exchange accounts. But, I'm seeing that it does with New emails and not with replies or forwards.

    Monday, January 28, 2019 12:57 PM