如何基于“到"过滤 VBA 中的 Outlook 视图电子邮件地址 [英] How to filter an Outlook view in VBA based on "To" email addresses

查看:58
本文介绍了如何基于“到"过滤 VBA 中的 Outlook 视图电子邮件地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Outlook 中有一个视图,其中包含来自邮箱的发件人"和收件人"电子邮件

I have a view in Outlook that includes both "From" and "To" emails from a mailbox

我可以非常轻松地在 Outlook 搜索框中输入电子邮件地址,该视图将立即被过滤以显示所有来自该电子邮件地址和来自该电子邮件地址的电子邮件(可能还包括主题中包含此电子邮件地址的其他任何电子邮件身体,但这不是问题)

I can very easily type in an email address into the Outlook search box, and the view will be instantly filtered to show all emails to and from the email address (plus perhaps, any other emails including this email address in the subject or body, but that's not an issue)

我想通过 VBA 复制此过程 - 我有一个包含联系详细信息的 Access DB,并且希望能够根据 DB 中的联系人电子邮件地址简单地过滤此视图.

I would like to replicate this process via VBA - I have an Access DB with contact details, and would like to be able to simply filter this view based on a contact email address in the DB.

这似乎是一个非常简单的问题,但我找不到解决方案.我有代码,例如基于发件人电子邮件地址进行过滤,但似乎无法过滤收件人"电子邮件地址(我可以过滤收件人"显示名称,但这几乎没用 - 它因一封电子邮件而异到另一个,并且很少包含实际的电子邮件地址)

It seems like a very simple problem, but I can't find the solution. I have code that eg filters based on the from email address, but there seems no way to filter on a 'To' email address (I can filter on the 'To' display name but this is next to useless - it varies from one email to another, and seldom if ever contains the actual email address)

我有一个 DASL 过滤器代码,可以使用 DASL 语法过滤发件人"电子邮件地址,如下所示:

I have a DASL filter code that works to filter on the 'From' email addresses using DASL syntax as follows:

Const SchemaFrom As String = "urn:schemas:httpmail:fromemail"
Dim EM as string
EM = "myemail@me.com"
objView.Filter = Chr(34) & SchemaFrom & Chr(34) & " = '" & EM & "'"

但收件人"电子邮件没有类似之处.

But there is nothing similar for 'To' emails.

最终我什至不需要根据例如发件人"和收件人"电子邮件地址进行过滤 - 如果有办法对此进行编码,那么,就像在搜索框中一样,它只需过滤任何文本字段包含我可以接受的电子邮件地址!!!

Ultimately I don't even need to filter based on eg the 'From' and 'To' email addresses - if there is a way to code this, so, like in the search box, it simply filters on any text field containing the email address that would be fine by me!!!

例如,是否有一个通用的 DASL 搜索可以做到这一点,所以不需要在例如 fromemail 中指定搜索?

For example, is there a generic DASL search that will do this, so no need to eg dictate searching in eg fromemail?

非常感谢所有帮助 - 在 Outlook 的 GUI 中执行如此简单,肯定可以从 VBA 中实现吗?!

All help much appreciated - it is so simple to do in the GUI of Outlook it must be possible from VBA surely?!

推荐答案

应该

Dim Filter As String
    Filter = "@SQL=" & Chr(34) & "urn:schemas:httpmail:fromemail" & _
                       Chr(34) & " Like '%0m3r 0m3r%'"

或者使用

Filter = "[SenderEmailAddress] = '0m3r@email.com'"

示例

Option Explicit
Private Sub Examples()
    Dim olNs As Outlook.NameSpace
    Dim Inbox As Outlook.MAPIFolder
    Dim Item As Outlook.MailItem
    Dim Items As Outlook.Items
    Dim msg As String

    Set olNs = Application.GetNamespace("MAPI")
    Set Inbox = olNs.GetDefaultFolder(olFolderInbox)

    Dim Filter As String
        Filter = "@SQL=" & Chr(34) & "urn:schemas:httpmail:fromemail" & _
                           Chr(34) & " Like '%0m3r 0m3r%'"

    Set Items = Inbox.Items.Restrict(Filter)

    msg = Items.Count & " Items in " & Inbox.Name

    MsgBox (msg)

End Sub

MSDN fromemail 字段 |Microsoft 文档

https://stackoverflow.com/search?tab=votes&q=user%3a4539709%20%5boutlook-filter%5d

这篇关于如何基于“到"过滤 VBA 中的 Outlook 视图电子邮件地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆