开发者

How can I create a custom sortable field in Outlook populated by a formula?

Having successfully created a custom field in Outlook via a formula and parsing the Subject field; I have now hit a new snag. The custom field is not sortable. It appears as of current that the only way to achieve this is to create a new user property which would then be sortable and could be leveraged when defining the field.

The option also exists to push everything into either a VBA script or C# app using the interop. Either way would work for me however I would prefer to go the VBA route and keep it self contained.

The emails exist in a folder and can be ran after the fact; the solution does not need to remain constantly active.

If anyone could point me to code which can easily achieve this that would be great. If I am missing an option to make the custom field sortable then please provide the alternative as that is the end goal.

EDIT:

This is what I currently have...

Sub SortCustomField()

    Dim olApp As Outlook.Application
    Dim objLotusInbox As Outlook.MAPIFolder
    Dim objLotusInboxItems As Outlook.Items
    Dim objNameSpace As Outlook.NameSpace
    Dim objProperty As Outlook.UserDefinedProperty


    Set olApp = CreateObject("Outlook.Application")
    Set objNameSpace = olApp.GetNamespace("MAPI")
    Set objLotusInbox = objNameSpace.GetDefaultFolder(olFolderInbox).Folders("Lotus Notes Inbox")

    Set objLotusInboxItems = objLotusInbox.Items
    objLotusInboxItems.Sort "[Notes2Outlook Created]", False

    Set objLotusInboxItems = Nothing
    Set objLotusInbox = Nothing
    Set objNameSpace = Nothing
    Set olApp = Nothing

End Sub

Erroring out at the sort; pretty certain it is due to the fact that the stated field is a user defined field as it works on other fields such as From.

UPDATE:

Made some ground, however when going back to Outlook it does not appear to populate the field defined during the run.

    Dim olApp As Outlook.Application
    Dim objLotusInbox As Outlook.MAPIFolder
    Dim objLotusInboxItems As Outlook.Items
    Dim objNameSpace As Outlook.NameSpace
    Dim objMailProperty As Outlook.UserProperty
    Dim objMailItem As Outlook.MailItem
    Dim objParsedDate As Date
    Dim sample As Object

    Set olApp = CreateObject("Outlook.Application")
    Set objNameSpace = olApp.GetNamespace("MAPI")
    Set objLotusInbox = objNameSpace.GetDefaultFolder(olFolderInbox).Folders("Lotus Notes Inbox")

    Set objLotusInboxItems = objLotusInbox.Items

    For Each objMailItem In objLotusInboxItems
        Set objMailProperty = objMailItem.UserProperties.Add("MyUserProp", olDateTime)
        objParsedDate = CDate(Mid(objMailItem.Subject, (InStr(objMailItem.Subject, "[") + 1), (InStr(objMailItem.Subject, "]") - InStr(objMailItem.Subject, "[")) - 1))
        objMailProperty.Value = objParsedDate
    Next

    Set objLotusI开发者_运维技巧nboxItems = Nothing
    Set objLotusInbox = Nothing
    Set objNameSpace = Nothing
    Set olApp = Nothing


I was having the same problem using c# and Add-in-express VSTO addin.

I solved it by saving the MailItem object after I has made the property change.

So for your code I would do the following:objMailItem.Save at the pertinent point. Namely at the end of each iteration of the For loop.

NB: In my c# code I use Marshal.ReleaseComObject for the USerProperty assigned object.


Since you are populating it in the VBA already you should be able to change the User-Property to a text field which can be sorted.

The method I use is running a script based on a rule but in your case you can call it as a sub in the for-each loop.

Sub SomeAction(Item As Outlook.MailItem)
    Dim myProperty As Outlook.UserProperty

    Set myProperty = Item.UserProperties.Add("MyUserProp", olText, True)
    myProperty.Value = Mid(objMailItem.Subject, (InStr(objMailItem.Subject, "[") + 1), (InStr(objMailItem.Subject, "]") - InStr(objMailItem.Subject, "[")) - 1))
    Item.Save

    Set myProperty = Nothing
End Sub

Only thing left to do is add a user defined column in your view which is a Text based user defined field.

note, since you are using a Date/Time item, this method should work just as well when you define the UserProperty as olDateTime


I've combined the answers from above to add a user defined column which is populated by running a rule selecting the script. I can then sort my inbox by the senders domain. My thanks to the previous contributors.

    Public Sub SortByDomain(oMsg As Outlook.MailItem)
    On Error Resume Next

    Dim sDomain As String 'The Sender's domain
    Dim oNS As Outlook.NameSpace 'My namespace
    Dim oInbox As Outlook.MAPIFolder 'My Inbox
    Dim oTarget As Outlook.MAPIFolder 'The domain folder
    Dim myProperty As Outlook.UserProperty


    'If it's not your domain, decipher the domain.
    If InStr(oMsg.SenderEmailAddress, "mydomain.com") < 1 Then
    sDomain = Mid(oMsg.SenderEmailAddress, InStr(oMsg.SenderEmailAddress, "@") + 1)
    Else
    sDomain = "mydomain.com"
    End If


    Set myProperty = oMsg.UserProperties.Add("SenderDomain", olText, True)
        myProperty.Value = sDomain
        oMsg.Save

    'Cleanup.
    Set oTarget = Nothing
    Set oInbox = Nothing
    Set oNS = Nothing
    Set myProperty = Nothing
    End Sub
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜