Outlook 中的 VBA 搜索 [英] VBA Search in Outlook

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

问题描述

我有这个代码可以在我的文件夹中搜索.我确实有一封带有草图"主题的电子邮件,但 VBA 没有找到它(它转到了 ELSE 子句)

I have this code to search in my folder. I do have a e-mail with the "sketch" subject, but VBA is not finding it (it goes to the ELSE clause)

谁能告诉我哪里出了问题?

Can anybody tell what is wrong ?

Set olApp = GetObject(, "Outlook.Application")

Set olNs = olApp.GetNamespace("MAPI")
Set olFldr = olNs.GetDefaultFolder(olFolderInbox)
Set olItms = olFldr.Items

Set Mail = olItms.Find("[Subject] = ""*sketch*""") 'Tracking
If Not (Mail Is Nothing) Then
    'use mail item here

Else
    NoResults.Show
End If

推荐答案

.Find 不工作的原因是 Items.Find 不支持通配符的使用.Items.Find 也不支持搜索部分字符串.因此,要真正找到电子邮件,您需要删除通配符并在搜索条件中包含整个字符串.

The reason your .Find isn't working is because Items.Find doesn't support the use of wildcards. Items.Find also doesn't support searching partial strings. So to actually find the email, you'd need to remove the wildcards and include the entire string in your search criteria.

这里是您的选择:

如果您知道要查找的完整主题行,请像这样修改您的代码:

If you know the full subject line you're looking for, modify your code like so:

Set Mail = olItms.Find("[Subject] = ""This Sketch Email""")

如果您不(或不会)知道完整的主题,您可以循环浏览收件箱文件夹并搜索部分主题行,如下所示:

If you don't (or won't) know the full subject, you can loop through your inbox folder and search for a partial subject line like so:

未经测试

Sub Search_Inbox()

Dim myOlApp As New Outlook.Application
Dim myNameSpace As Outlook.NameSpace
Dim myInbox As Outlook.MAPIFolder
Dim myitems As Outlook.Items
Dim myitem As Object
Dim Found As Boolean

Set myNameSpace = myOlApp.GetNamespace("MAPI")
Set myInbox = myNameSpace.GetDefaultFolder(olFolderInbox)
Set myitems = myInbox.Items
Found = False

For Each myitem In myitems
    If myitem.Class = olMail Then
        If InStr(1, myitem.Subject, "sketch") > 0 Then
            Debug.Print "Found"
            Found = True
        End If
    End If
Next myitem

'If the subject isn't found:
If Not Found Then
    NoResults.Show
End If

myOlApp.Quit
Set myOlApp = Nothing

End Sub

希望有帮助!

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

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