用于在Outlook邮件中进行搜索的Excel VBA [英] Excel VBA for searching in mails of 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屋!