如何在由公式填充的Outlook中创建自定义可排序字段?

时间:2011-04-21 17:33:58

标签: c# vba outlook office-interop

通过公式在Outlook中成功创建自定义字段并解析“主题”字段;我现在遇到了一个新问题。自定义字段不可排序。目前看来,实现这一目标的唯一方法是创建一个新的user property,然后可以对其进行排序,并在定义字段时加以利用。

该选项还可以使用互操作将所有内容推送到VBA脚本或C#应用程序中。无论哪种方式对我都有用,但是我更愿意选择VBA路线并保持自包含。

电子邮件存在于一个文件夹中,可以在事后运行;解决方案不需要保持持续活跃。

如果有人能指出我可以轻松实现这一目标的代码,那就太好了。如果我错过了使自定义字段可排序的选项,请提供替代方案,因为这是最终目标。

编辑:

这就是我现在所拥有的......

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

排序错误;很确定这是因为声明的字段是用户定义的字段,因为它适用于其他字段,例如From

更新

取得了一些进展,但是当回到Outlook时,似乎没有填充在运行期间定义的字段。

    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 objLotusInboxItems = Nothing
    Set objLotusInbox = Nothing
    Set objNameSpace = Nothing
    Set olApp = Nothing

3 个答案:

答案 0 :(得分:1)

使用c#和Add-in-express VSTO addin我遇到了同样的问题。

我通过在更改属性后保存MailItem对象来解决它。

因此,对于您的代码,我会在相关点执行以下操作:objMailItem.Save。即在For循环的每次迭代结束时。

注意:在我的c#代码中,我使用Marshal.ReleaseComObject作为USerProperty分配的对象。

答案 1 :(得分:1)

由于您已经在VBA中填充它,因此您应该能够将User-Property更改为可以排序的文本字段。

我使用的方法是根据规则运行脚本,但在您的情况下,您可以将其称为for-each循环中的sub。

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

唯一要做的就是在视图中添加一个用户定义的列,这是一个基于文本的用户定义字段。

请注意,由于您使用的是日期/时间项,因此当您将UserProperty定义为olDateTime时,此方法也可以正常工作

答案 2 :(得分:1)

我结合了上面的答案来添加用户定义的列,该列通过运行选择脚本的规则来填充。然后我可以通过发件人域对我的收件箱进行排序。我要感谢以前的贡献者。

    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