Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Outlook: Add User Property when Item added - reliably with exchange host server?

Status
Not open for further replies.

lameid

Programmer
Jan 31, 2001
4,208
US
I use a user property ("ReceivedDateOnly") to group on a received date in MM/dd/yyyy format and then subsequently sort on the real received date sorted on time hh:mm format.

The below code was usually reliable in Outlook 2013. Since migrating to Outlook 2016, I have had a lot of problems.

I know there is an alternate method that is allegedly better for handling processing the larger batches of e-mails that happens with an Exchange host but I lost my note and while this is an enormous pain in my side, I don't have the time to research it...

I now have more send/receives where it fails to populate the user property than successes even small batches. I have blown away my OST and reran everything which seemed to help for a few days and now back to the same behavior.

Aside from code, any other insights that I can take to the Help desk would be welcome too.

Code:
Option Explicit

Private WithEvents Items As Outlook.Items
Private Sub Application_Startup()
  Dim olApp As Outlook.Application
  Dim objNS As Outlook.NameSpace
  Set olApp = Outlook.Application
  Set objNS = olApp.GetNamespace("MAPI")
  ' default local Inbox
  Set Items = objNS.GetDefaultFolder(olFolderInbox).Items
End Sub

Private Sub Items_ItemAdd(ByVal Item As Object)

  On Error GoTo ErrorHandler
  Dim dtReceived As Date
  Dim objProp As Outlook.UserProperty
    
  Select Case TypeName(Item)
    Case "ReportItem" 'NDR's - Non Delivery Reports
        dtReceived = Item.CreationTime 'Looks like this is the "Received Date"
    Case Else 'MailItem and MeetingItem I definitely saw working
      dtReceived = Item.ReceivedTime
  End Select
  'MsgBox "Date Received is" & dtReceived 'Yes it is running
 'Property added below needs added to inbox view if you want to see or group on it
  Set objProp = Item.UserProperties.Add("ReceivedDateOnly", olDateTime, True)
  objProp.Value = DateSerial(Year(dtReceived), Month(dtReceived), Day(dtReceived))
  Item.Save 'item needs saved now that it has been updated
ProgramExit:
  Exit Sub
ErrorHandler:
  MsgBox Err.Number & " - " & Err.Description
  Resume ProgramExit
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top