Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在Outlook中创建由公式填充的自定义可排序字段?_C#_Vba_Outlook_Office Interop - Fatal编程技术网

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

C# 如何在Outlook中创建由公式填充的自定义可排序字段?,c#,vba,outlook,office-interop,C#,Vba,Outlook,Office Interop,通过公式在Outlook中成功创建自定义字段并解析主题字段;我现在遇到了一个新的障碍。自定义字段不可排序。目前看来,实现这一点的唯一方法是创建一个新的可排序字段,并在定义字段时加以利用 该选项还可以使用互操作将所有内容推送到VBA脚本或C#app中。无论哪种方式对我都有效,但我更愿意走VBA路线,并保持其独立性 电子邮件存在于文件夹中,可以在事后运行;解决方案不需要一直保持活动状态 如果有人能给我指出一些代码,这些代码可以很容易地实现这一点,那就太好了。如果我缺少使自定义字段可排序的选项,那么请

通过公式在Outlook中成功创建自定义字段并解析主题字段;我现在遇到了一个新的障碍。自定义字段不可排序。目前看来,实现这一点的唯一方法是创建一个新的可排序字段,并在定义字段时加以利用

该选项还可以使用互操作将所有内容推送到VBA脚本或C#app中。无论哪种方式对我都有效,但我更愿意走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
在分类上出错;可以肯定的是,这是因为声明的字段是用户定义的字段,因为它适用于其他字段,例如来自的

更新:

但是,在返回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

我在使用c#和加载项express VSTO addin时遇到了同样的问题

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

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


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

我在使用c代码和加载项express VSTO加载项时遇到了相同的问题

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

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


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

既然您已经在VBA中填充了它,您应该能够将User属性更改为可以排序的文本字段

我使用的方法是基于规则运行脚本,但在您的情况下,可以在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时,此方法应同样有效,因为您已经在VBA中填充了UserProperty,您应该能够将UserProperty更改为可排序的文本字段

我使用的方法是基于规则运行脚本,但在您的情况下,可以在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时,此方法也应同样有效。我将上面的答案结合起来,添加了一个用户定义的列,该列通过运行选择脚本的规则来填充。然后,我可以按发件人域对收件箱进行排序。我要感谢以前的贡献者

    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
Public子排序域(oMsg作为Outlook.MailItem)
出错时继续下一步
Dim sDomain作为字符串“发件人的域”
Dim oNS作为Outlook.NameSpace的“我的命名空间”
将收件箱设置为Outlook.MAPIFolder“我的收件箱”
将目标设置为Outlook.MAPIFolder“域文件夹”
将myProperty设置为Outlook.UserProperty
'如果不是您的域,请破译该域。
如果InStr(oMsg.SenderEmailAddress,“mydomain.com”)<1,则
sDomain=Mid(oMsg.SenderEmailAddress,InStr(oMsg.SenderEmailAddress,“@”)+1)
其他的
sDomain=“mydomain.com”
如果结束
Set myProperty=oMsg.UserProperties.Add(“SenderDomain”,olText,True)
myProperty.Value=sDomain
oMsg.保存
“清理。
设置oTarget=Nothing
设置框=无
设为零=零
设置myProperty=Nothing
端接头

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

    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
Public子排序域(oMsg作为Outlook.MailItem)
出错时继续下一步
Dim sDomain作为字符串“发件人的域”
Dim oNS作为Outlook.NameSpace的“我的命名空间”
将收件箱设置为Outlook.MAPIFolder“我的收件箱”
将目标设置为Outlook.MAPIFolder“域文件夹”
将myProperty设置为Outlook.UserProperty
'如果不是您的域,请破译该域。
如果InStr(oMsg.SenderEmailAddress,“mydomain.com”)<1,则
sDomain=Mid(oMsg.SenderEmailAddress,InStr(oMsg.SenderEmailAddress,“@”)+1)
其他的
sDomain=“mydomain.com”
如果结束
Set myProperty=oMsg.UserProperties.Add(“SenderDomain”,olText,True)
myProperty.Value=sDomain
oMsg.保存
“清理。
设置oTarget=Nothing
设置框=无
设为零=零
设置myProperty=Nothing
端接头

我仍然需要尝试这个方法,这样我就可以验证它是否确实有效,我已经通过了这项工作,但我希望在验证后将答案授予您。我仍然需要尝试这个方法,这样我就可以验证它是否确实有效,我已经通过了这项工作,但我希望在验证后将答案授予您。