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

发布于 2024-11-02 21:40:25 字数 2446 浏览 3 评论 0原文

通过公式在 Outlook 中成功创建自定义字段并解析主题字段;我现在遇到了新的障碍。自定义字段不可排序。目前看来,实现此目的的唯一方法是创建一个新的 用户属性 然后可以对其进行排序,并可以在定义字段时使用。

该选项还可以使用互操作将所有内容推送到 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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夏至、离别 2024-11-09 21:40:25

我在使用 C# 和 Add-in-express VSTO 插件时遇到了同样的问题。

我通过在更改属性后保存 MailItem 对象来解决这个问题。

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

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

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.

嘴硬脾气大 2024-11-09 21:40:25

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

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

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 时,此方法应该同样有效

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

神回复 2024-11-09 21:40:25

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

    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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文