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

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

问题描述

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

  Sub Work_with_Outlook()

设置outlookApp = CreateObject(Outlook.Application )

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

设置outlookApp =新的Outlook.Application
设置olNs = outlookApp.GetNamespace(MAPI)
设置Fldr = olNs.GetDefaultFolder(olFolderInbox)
设置myTasks = Fldr.Items

设置olMail = myTasks.Find([Subject] =123456)
如果不是(olMail不是)然后
olMail.Display
End If

End Sub


解决方案

你不能在查找(过滤器)中使用正文,请参阅 Items.Find方法(Outlook)
作为解决方法,您可以使用VBA字符串搜索功能:

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

'设置outlookApp =新的Outlook.Application
设置outlookApp = CreateObject(Outlook.Application)

设置olNs = outlookApp.GetNamespace( MAPI)
设置Fldr = olNs.GetDefaultFolder(olFolderInbox)
设置myTasks = Fldr.Items

'
'设置olMail = myTasks.Find([Subject ] =123456)
'
对于每个olMail在myTasks
'
如果(InStr(1,olMail.Body,My-Text-to-Search ,vbTextCompare)> 0)然后
olMail.Display
退出
结束如果
下一个

结束子


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

解决方案

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天全站免登陆