使用 urn:schemas 按电子邮件地址搜索

2023-12-15

I found 这段代码来自里卡多·迪亚兹。它贯穿始终。

我想搜索我收到或发送到特定的最新电子邮件电子邮件地址与搜索相反subject.

我更换了

searchString = "urn:schemas:httpmail:subject like '" & emailSubject & "'"

with

searchString = "urn:schemas:httpmail:to like '" & emailSubject & "'"

搜索返回一个空对象。

在 Outlook 收件箱和已发送邮件中搜索发件人和收件人电子邮件地址的 urn:schemas 是什么?

这是我试图运行的代码:

在 VBA 模块中:

Public Sub ProcessEmails()
    
    Dim testOutlook As Object
    Dim oOutlook As clsOutlook
    Dim searchRange As Range
    Dim subjectCell As Range
    
    Dim searchFolderName As String
        
    ' Start outlook if it isn't opened (credits: https://stackoverflow.com/questions/33328314/how-to-open-outlook-with-vba)
    On Error Resume Next
    Set testOutlook = GetObject(, "Outlook.Application")
    On Error GoTo 0
    
    If testOutlook Is Nothing Then
        Shell ("OUTLOOK")
    End If
    
    ' Initialize Outlook class
    Set oOutlook = New clsOutlook
    
    ' Get the outlook inbox and sent items folders path (check the scope specification here: https://learn.microsoft.com/en-us/office/vba/api/outlook.application.advancedsearch)
    searchFolderName = "'" & Outlook.Session.GetDefaultFolder(olFolderInbox).FolderPath & "','" & Outlook.Session.GetDefaultFolder(olFolderSentMail).FolderPath & "'"
    
    ' Loop through excel cells with subjects
    Set searchRange = ThisWorkbook.Worksheets("Sheet1").Range("A2:A4")
    
    For Each subjectCell In searchRange
        
        ' Only to cells with actual subjects
        If subjectCell.Value <> vbNullString Then
        
            Call oOutlook.SearchAndReply(subjectCell.Value, searchFolderName, False)
        
        End If
    
    Next subjectCell
    
    MsgBox "Search and reply completed"
    
    ' Clean object
    Set testOutlook = Nothing

End Sub

在名为 clsOutlook 的类模块中:

Option Explicit

' Credits: Based on this answer: https://stackoverflow.com/questions/31909315/advanced-search-complete-event-not-firing-in-vba

' Event handler for outlook
Dim WithEvents OutlookApp As Outlook.Application
Dim outlookSearch As Outlook.Search
Dim outlookResults As Outlook.Results

Dim searchComplete As Boolean

' Handler for Advanced search complete
Private Sub outlookApp_AdvancedSearchComplete(ByVal SearchObject As Search)
    'MsgBox "The AdvancedSearchComplete Event fired."
    searchComplete = True
End Sub


Sub SearchAndReply(emailSubject As String, searchFolderName As String, searchSubFolders As Boolean)
    
    ' Declare objects variables
    Dim customMailItem As Outlook.MailItem
    Dim searchString As String
    Dim resultItem As Integer
    
    ' Variable defined at the class level
    Set OutlookApp = New Outlook.Application
    
    ' Variable defined at the class level (modified by outlookApp_AdvancedSearchComplete when search is completed)
    searchComplete = False
    
    ' You can look up on the internet for urn:schemas strings to make custom searches
    searchString = "urn:schemas:httpmail:to like '" & emailSubject & "'"
    
    ' Perform advanced search
    Set outlookSearch = OutlookApp.AdvancedSearch(searchFolderName, searchString, searchSubFolders, "SearchTag")
    
    ' Wait until search is complete based on outlookApp_AdvancedSearchComplete event
    While searchComplete = False
        DoEvents
    Wend
    
    ' Get the results
    Set outlookResults = outlookSearch.Results
    
    If outlookResults.Count = 0 Then Exit Sub
    
    ' Sort descending so you get the latest
    outlookResults.Sort "[SentOn]", True
    
    ' Reply only to the latest one
    resultItem = 1
        
    ' Some properties you can check from the email item for debugging purposes
    On Error Resume Next
    Debug.Print outlookResults.Item(resultItem).SentOn, outlookResults.Item(resultItem).ReceivedTime, outlookResults.Item(resultItem).SenderName, outlookResults.Item(resultItem).Subject
    On Error GoTo 0
        
    Set customMailItem = outlookResults.Item(resultItem).ReplyAll
    
    ' At least one reply setting is required in order to replyall to fire
    customMailItem.Body = "Just a reply text " & customMailItem.Body
    
    customMailItem.Display
    
End Sub

Sheet1 中的单元格 A2:A4 包含电子邮件地址,例如[电子邮件受保护]例如。


您可以通过另一种方式获得看似“urn:schemas:httpmail:to”的内容。
读取 Outlook 对象模型中未公开的 MAPI 属性

其有用性仍有待证明,因为来自地址相关属性的值要么不可用,要么微不足道。

Option Explicit

' https://www.slipstick.com/developer/read-mapi-properties-exposed-outlooks-object-model/
Const PR_RECEIVED_BY_NAME As String = "http://schemas.microsoft.com/mapi/proptag/0x0040001E"
Const PR_SENT_REPRESENTING_NAME As String = "http://schemas.microsoft.com/mapi/proptag/0x0042001E"

Const PR_RECEIVED_BY_EMAIL_ADDRESS As String = "http://schemas.microsoft.com/mapi/proptag/0x0076001E"
Const PR_SENT_REPRESENTING_EMAIL_ADDRESS As String = "http://schemas.microsoft.com/mapi/proptag/0x0065001E"
Const PR_SENDER_EMAIL_ADDRESS As String = "http://schemas.microsoft.com/mapi/proptag/0x0C1F001E"

Sub ShowPropertyAccessorValue()

    Dim oItem As Object
    Dim propertyAccessor As outlook.propertyAccessor
    
    ' for testing
    ' select an item from any folder not the Sent folder
    '  then an item from the Sent folder
    Set oItem = ActiveExplorer.Selection.item(1)
    
    If oItem.Class = olMail Then
    
        Set propertyAccessor = oItem.propertyAccessor
        
        Debug.Print
        Debug.Print "oItem.Parent......................: " & oItem.Parent
        
        Debug.Print "Sender Display name...............: " & oItem.Sender
        Debug.Print "Sender address....................: " & oItem.SenderEmailAddress
            
        Debug.Print "PR_RECEIVED_BY_NAME...............: " & _
          propertyAccessor.GetProperty(PR_RECEIVED_BY_NAME)
        Debug.Print "PR_SENT_REPRESENTING_NAME.........: " & _
          propertyAccessor.GetProperty(PR_SENT_REPRESENTING_NAME)
        
        Debug.Print "PR_RECEIVED_BY_EMAIL_ADDRESS......: " & _
          propertyAccessor.GetProperty(PR_RECEIVED_BY_EMAIL_ADDRESS)
        Debug.Print "PR_SENT_REPRESENTING_EMAIL_ADDRESS: " & _
          propertyAccessor.GetProperty(PR_SENT_REPRESENTING_EMAIL_ADDRESS)
        Debug.Print "PR_SENDER_EMAIL_ADDRESS...........: " & _
          propertyAccessor.GetProperty(PR_SENDER_EMAIL_ADDRESS)
        
    End If
End Sub

示例格式来自使用字符串比较过滤项目

Private Sub RestrictBySchema()

    Dim myInbox As Folder
    Dim myFolder As Folder
    
    Dim propertyAccessor As propertyAccessor
    
    Dim strFilter As String
    Dim myResults As Items
     
    Dim mailAddress As String
        
    ' for testing
    ' open any folder not the Sent folder
    '  then the Sent folder
    Set myFolder = ActiveExplorer.CurrentFolder
    
    Debug.Print "myFolder............: " & myFolder
    Debug.Print "myFolder.items.Count: " & myFolder.Items.Count
    
    mailAddress = "[email protected]"
    
    Debug.Print "mailAddress: " & mailAddress
    
    ' Filtering Items Using a String Comparison
    ' https://learn.microsoft.com/en-us/office/vba/outlook/how-to/search-and-filter/filtering-items-using-a-string-comparison
    'strFilter = "@SQL=""https://schemas.microsoft.com/mapi/proptag/0x0037001f"" = 'the right ""stuff""'"
    'Debug.Print "strFilter .....: " & strFilter
    
    ' Items where PR_RECEIVED_BY_EMAIL_ADDRESS = specified email address
    '  This is the To
    '  No result from the Sent folder
    '  Logical as the item in the Sent folder could have multiple receivers
    Debug.Print
    Debug.Print "PR_RECEIVED_BY_EMAIL_ADDRESS"
    strFilter = "@SQL=" & """" & PR_RECEIVED_BY_EMAIL_ADDRESS & """" & " = '" & mailAddress & "'"
    Debug.Print "strFilter .....: " & strFilter
    Set myResults = myFolder.Items.Restrict(strFilter)
    Debug.Print " myResults.Count.....: " & myResults.Count
    
    ' Items where PR_SENT_REPRESENTING_EMAIL_ADDRESS = specified email address
    Debug.Print
    Debug.Print "PR_SENT_REPRESENTING_EMAIL_ADDRESS"
    strFilter = "@SQL=" & """" & PR_SENT_REPRESENTING_EMAIL_ADDRESS & """" & " = '" & mailAddress & "'"
    Debug.Print "strFilter .....: " & strFilter
    Set myResults = myFolder.Items.Restrict(strFilter)
    Debug.Print " myResults.Count.....: " & myResults.Count
    
    ' Items where SenderEmailAddress = specified email address
    Debug.Print
    Debug.Print "SenderEmailAddress"
    strFilter = "[SenderEmailAddress] = '" & mailAddress & "'"
    Debug.Print "strFilter .....: " & strFilter
    Set myResults = myFolder.Items.Restrict(strFilter)
    Debug.Print " myResults.Count.....: " & myResults.Count
    
    ' Items where PR_SENDER_EMAIL_ADDRESS = specified email address
    Debug.Print
    Debug.Print "PR_SENDER_EMAIL_ADDRESS"
    strFilter = "@SQL=" & """" & PR_SENDER_EMAIL_ADDRESS & """" & " = '" & mailAddress & "'"
    Debug.Print "strFilter .....: " & strFilter
    Set myResults = myFolder.Items.Restrict(strFilter)
    Debug.Print " myResults.Count.....: " & myResults.Count
    
End Sub
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 urn:schemas 按电子邮件地址搜索 的相关文章

随机推荐

  • 从数组中删除重复项

    您好 我有一个使用此函数从 XML 文件创建的数组 LOCATIONS XML HANDLER creates array holding values of field selected from XML string xml param
  • 是否可以从 android 调用 WSHTTPBINDING?

    private static final String SOAP ACTION http tempuri org IService1 HelloTest private static final String METHOD NAME Hel
  • 如何使用 C# 禁用文本框上的复制、粘贴和删除功能

    有人可以建议如何使用 C 处理 WinForms 中文本框上的剪切 复制和粘贴事件吗 在 WinForms 中 禁用文本框上的剪切 复制和粘贴功能的最简单方法是将 ShortcutsEnabled 属性设置为 false
  • 如何在 MDX 中使用 UNION

    我想要UNION下面MDX询问 对于这两个查询 同一日期范围的度量和维度是不同的 请帮助我摆脱困境 SELECT NON EMPTY Measures Number of es2 ON COLUMNS NON EMPTY Date Year
  • 如何按升序对奇数数组进行排序,但将偶数保留在其位置?

    我只想对奇数进行排序而不移动偶数 例如 当我写 sortArray 5 3 2 8 1 4 预期结果是 1 3 2 8 5 4 我是 JavaScript 新手 在互联网上遇到了一个令我困惑的挑战 我通常不会在互联网上发布请求解决方案 但我
  • 带yield的递归函数不返回任何内容

    我正在尝试创建一个用于排列目的的生成器 我知道在 Python 中还有其他方法可以做到这一点 但这是为了别的事情 不幸的是 我无法产生这些值 你能帮我吗 def perm s p 0 ii 0 l len s s list s if l 1
  • HTML5 音频加载

    当音频标签准备好播放时如何获得回调 在实现我自己的控件时告诉用户 使用 Chrome 仅在视频元素上执行此操作 但它应该适用于音频 首先 你不能绑定事件 我不知道为什么这不起作用 所以必须使用setTimeout 使用 jQuery 的示例
  • boost 中的共享内存向量

    我有以下代码 尝试拥有一个具有字符串和数组结构的共享内存向量 但是当我编译代码时出现错误 usr local include boost container vector hpp 1819 4 error no matching funct
  • 在标题中隐藏类类型

    我不确定这是否可能 但这里是 我有一个库 其界面充其量是复杂的 不幸的是 它不仅是一个第三方库 而且太大而无法重写 而且我还使用了一些依赖于它的其他库 因此该界面必须保持原样 为了解决这个问题 我试图从本质上包装接口并将所有依赖项的接口捆绑
  • OPOS PosExplorer.GetDevice() 在 Windows 服务中执行时返回 null

    下面的代码片段利用 OPOS NET 打开 POS 打印机进行打印 当作为独立应用程序的一部分执行时 它工作得很好 当由 Windows 服务执行时 对 GetDevice 的调用始终返回 null explorer new PosExpl
  • Spring boot应用程序启动时如何缓存数据

    我有一个连接到 SQL Server 数据库的 Spring boot 应用程序 我需要一些帮助来在我的应用程序中使用缓存 我有一个 CodeCategory 表 其中包含许多代码的代码列表 该表将每月加载一次 并且数据每月仅更改一次 我想
  • Inno安装程序更改Setup.tmp的任务管理器描述

    我已经怀疑这是不可能的 并且可能是硬编码的 需要修改 Inno Setup 源代码 但是 Inno Setup 非常灵活 所以也许有办法吗 是否可以将与Setup exe 一起运行的Setup tmp 的Windows 任务管理器描述字段中
  • 在 Objective-C 中获取 NSURL 的一部分

    我有一个 NSString 其值为 http digg com news business 24hr 如何获得第三级之前的所有内容 http digg com news 请注意 这不完全是第三层 URL 的分割方式如下 协议或方案 这里 h
  • PHP 会话*并不总是*有效

    我最近将一堆网站迁移到新的 VPS 但 PHP 会话遇到了很多问题 所有网站的配置方式几乎相同 但有一个网站的会话并不总是有效 让我烦恼的是它是随机的 我可以访问该网站并登录 并且会话将正常工作 然后 我一小时后返回 尝试使用同一帐户登录
  • 更改 Firemonkey TListView 中的交替颜色

    将 tlistview 拖放到表单上并检查属性 AlternatingColors 为 true 默认情况下它将在列表视图上的所有其他项目上显示灰色背景 我的问题是现在有人如何将此颜色更改为其他颜色吗 Thanks 您需要编辑 TlistV
  • 如何从给定的 inode 获取路径名或 dentry 或结构文件?

    我需要知道如何获取路径名或dentry or struct file来自给定的索引节点 我正在使用file open获得struct file来自路径名 但总是引起内核恐慌 我需要一种方法来将我的 inode 列表中的 inode 与路径名
  • Prolog 替换

    如何用包含要替换的变量的另一个列表替换一个列表 例如 rep x d e z x z p x z z x d c R R z c e x z x p x 到 z 和 z 在替换后不会改变 到目前为止我只做了没有清单的那一项 rep rep
  • 方法的 ES6 对象字面量语法

    我正在看这个这一页有关 ES6 中用于声明对象内部方法的各种简写语法 我不明白这两种形式之间的区别 var foo a b and var foo x y gt y 这篇文章似乎对这两种格式做了明确的区分 但是第一种格式不就真的变成了第二种
  • 带有 jQ​​uery 和 Masked 输入插件的电话掩码

    我在使用 jQuery 屏蔽电话输入时遇到问题屏蔽输入插件 有 2 种可能的格式 XX XXXX XXXX XX XXXXX XXXX 有什么办法可以掩盖它接受这两种情况吗 EDIT I tried phone mask 99 9999 9
  • 使用 urn:schemas 按电子邮件地址搜索

    I found 这段代码来自里卡多 迪亚兹 它贯穿始终 我想搜索我收到或发送到特定的最新电子邮件电子邮件地址与搜索相反subject 我更换了 searchString urn schemas httpmail subject like e