none
Event on close mail item does not work RRS feed

  • Question

  • When I generate a new mail from a custom button a variable is set. When I send that mail the event 'Application_ItemSend' pics this variable up and does something depending on the content of the variable.

    When I close that mail and save it as draft then that variable is not cleared.

    What can I do to clear that variable when I close that mail with or without saving?

    I have tried (in ThisOutlookSession):

    Public WithEvents myItem As Outlook.MailItem

    Private Sub myItem_Close(Cancel As Boolean)
        'do something
        MsgBox "mail closed"
    End Sub

    But does not seem to work.

    Please help

    Saturday, October 21, 2017 10:33 AM

Answers

  •   set Prop = MailItem.UserProperties.Add("MyTextProp", olText)
      Prop.Value = "Some text"
    
      set Prop = MailItem.UserProperties.Add("MyNumberProp", olNumber)
      Prop.Value = 123
    
      ...
      
      'at some later point...
      set Prop = MailItem.UserProperties.Find("MyTextProp")
      if Not (Prop Is Nothing) Then
        propValue = Prop.Value
      End If


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


    Sunday, October 22, 2017 5:14 PM

All replies

  • You have more problems than that - what if the user opens two messages at the same time? Which item would the variable apply to?

    You need to either have a class wrapper that stores the state of that variable on the per-item basis and processes all item related events or you can set a user property on the item itself and read it when the Application.ItemSend event fires.


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

    Saturday, October 21, 2017 6:09 PM
  • How can I assign a custom property to a new message? (my Excel vba skills are +/-ok but Outlook vba is a bit new for me)

    I have an outlook macro that creates some standard new messages. Some are sent immediately, others are saved in the draft folder (when closing the new mail). It would be great to assign some custom properties to those new mails.

    The custom properties would be something like: quote, price confirmation, order, transport, ...

    Is there an event that fires when mails from the draft folder are opened for editing?

    Then if the Application.ItemSend event could pick up those properties then :-)

    Sunday, October 22, 2017 7:36 AM
  •   set Prop = MailItem.UserProperties.Add("MyTextProp", olText)
      Prop.Value = "Some text"
    
      set Prop = MailItem.UserProperties.Add("MyNumberProp", olNumber)
      Prop.Value = 123
    
      ...
      
      'at some later point...
      set Prop = MailItem.UserProperties.Find("MyTextProp")
      if Not (Prop Is Nothing) Then
        propValue = Prop.Value
      End If


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


    Sunday, October 22, 2017 5:14 PM
  • Thank you Dmitry,

    This works great.

    Other question: how can I loop through all the userproperties? Not just the values of the specific userproperty but the "fields" themself?

    Like:

    set Prop = MailItem.UserProperties.Add("MyNumberProp", olNumber)
      Prop
    .Value = 123
    set Prop = MailItem.UserProperties.Add("MyText1Prop", olText)
      Prop
    .Value = "Text1"
    set Prop = MailItem.UserProperties.Add("MyText2Prop", olText)
      Prop
    .Value = "Long Text2"

    Set Prop = MailItem.UserProperties.Add("MyText3Prop", olText)
      Prop
    .Value = "Verry long text, perhaps a folder location"

    How do I run a loop and get "MyNumberProp", "MyText1Prop", "MyText2Prop", "MyText3Prop" as a result and not 123, "Text1", "Long Text2", "Verry long text, perhaps a folder location"

    Monday, October 23, 2017 8:42 AM
  • You can loop through the UserProperies collection using either "for" or "for each" loop.

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

    Monday, October 23, 2017 1:24 PM