用于在 Outlook 邮件中搜索的 Excel VBA [英] Excel VBA for searching in mails of Outlook

查看:34
本文介绍了用于在 Outlook 邮件中搜索的 Excel VBA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个项目,在该项目中我有一个帐户 ID 列表,我想要做的是创建一个与 Outlook 交互的宏,在我的收件箱中搜索具有特定条件的任何电子邮件,以及如果找到,则返回Y"或N",如果找到,则返回电子邮件的发送者和发送时间.下面是我正在使用的代码;我需要宏来搜索电子邮件的正文而不是主题行.当我用 [Subject] 替换 [Body] 时,宏运行没有错误,但不返回任何电子邮件(我放置了几个测试电子邮件以供捕获).我正在运行 Excel 和 Outlook 2007,并且已经参考了 MS 12.0 Excel &VBA 中的 Outlook 库.

I'm working on a project where I have a list of Account IDs, and what I'm trying to do is to create a macro that will interface with Outlook, search my Inbox for any email with an specific criteria, and then return "Y" or "N" if it was found, and if it was found, who the email was sent from and the time it was sent. Below is the code I'm using; I need the macro to search the Body of the Email instead of the Subject Line. When I substitute [Subject] for [Body], the macro runs without errors, but returns no emails (I place a couple test emails for it to catch). I am running Excel and Outlook 2007, and have already reference the MS 12.0 Excel & Outlook libraries in VBA.

Sub Work_with_Outlook()

    Set outlookApp = CreateObject("Outlook.Application")

    Dim olNs As Outlook.Namespace
    Dim Fldr As Outlook.MAPIFolder
    Dim olMail As Variant
    Dim sir() As String

    Set outlookApp = New Outlook.Application
    Set olNs = outlookApp.GetNamespace("MAPI")
    Set Fldr = olNs.GetDefaultFolder(olFolderInbox)
    Set myTasks = Fldr.Items

    Set olMail = myTasks.Find("[Subject] = ""123456""")
    If Not (olMail Is Nothing) Then
        olMail.Display
    End If

End Sub

推荐答案

您不能在 Find(Filter) 中使用 Body,请参阅 Items.Find 方法 (Outlook),作为解决方法,您可以使用 VBA 字符串搜索功能:

You cannot use Body in Find(Filter), see Items.Find Method (Outlook), as a Workaround, you can use VBA string search function:

Sub sofWorkWithOutlook20082550()
  Dim outlookApp
  Dim olNs As Outlook.Namespace
  Dim Fldr As Outlook.MAPIFolder
  Dim olMail As Variant
  Dim myTasks
  Dim sir() As String

  'Set outlookApp = New Outlook.Application
  Set outlookApp = CreateObject("Outlook.Application")

  Set olNs = outlookApp.GetNamespace("MAPI")
  Set Fldr = olNs.GetDefaultFolder(olFolderInbox)
  Set myTasks = Fldr.Items

  '
  'Set olMail = myTasks.Find("[Subject] = ""123456""")
  '
  For Each olMail In myTasks
  '
    If (InStr(1, olMail.Body, "My-Text-to-Search", vbTextCompare) > 0) Then
      olMail.Display
      Exit For
    End If
  Next

End Sub

这篇关于用于在 Outlook 邮件中搜索的 Excel VBA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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