Excel vba:循环浏览 Outlook 电子邮件中的所有子文件夹以查找具有特定主题的电子邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45346183/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 12:59:22  来源:igfitidea点击:

Excel vba: Looping through all subfolders in Outlook email to find an email with certain subject

excelvbaoutlook

提问by Trs

I have written the following code in Excel VBA that opens an email with the given subject if located in the default inbox folder in Outlook.

我已经在 Excel VBA 中编写了以下代码,如果位于 Outlook 的默认收件箱文件夹中,它会打开一封带有给定主题的电子邮件。

However, I would like to search for this email in all inbox subfolders.

但是,我想在所有收件箱子文件夹中搜索此电子邮件。

Because the code will be used by several users, I do not know the number and the name of their outlook inbox subfolders. Any ideas on how I could search this email in all subfolders?

因为代码将被多个用户使用,我不知道他们的 Outlook 收件箱子文件夹的数量和名称。关于如何在所有子文件夹中搜索此电子邮件的任何想法?

Sub GetEmail()

    Dim OutApp as Object
    Dim Namespace as Object
    Dim Folder as Object
    Dim myMail as Object

    Set OutApp = CreateObject("Outlook.Application")
    Set Namespace = OutApp.GetNamespace ("MAPI")
    Set Folder = Namespace.GetDefaultFolder(6)

    Set myMail = Folder.Items.Find ("[Subject] = ""Test""")

    myMail.Display


End Sub

回答by Keith Whatling

The below code cycles through all folders in Outlook, to the level one beneath the Inbox. You can just look at the inbox by specifying the initial folder to look at. Thus you can search the folder as you loop through. you can add further sub folders by looping deeper, or by saying folders.count > X.

下面的代码循环浏览 Outlook 中的所有文件夹,直到收件箱下方的一级。您可以通过指定要查看的初始文件夹来查看收件箱。因此,您可以在循环时搜索文件夹。您可以通过更深入地循环或说 folders.count > X 来添加更多子文件夹。

I have always found Outlook from Excel frustrating so have made this Early Bound to make coding easier. This means that you will need to go to Tool/References and add Microsoft Outlook 16(x).0 Object Library

我总是发现 Excel 中的 Outlook 令人沮丧,所以我做了这个早期绑定,使编码更容易。这意味着您需要转到工具/参考并添加 Microsoft Outlook 16(x).0 对象库

You can change it back to late bound after coding, as early binding will give you IntelliSense and make life a whole lot easier.

您可以在编码后将其改回后期绑定,因为早期绑定将为您提供智能感知并使生活变得更加轻松。

Sub GetEmail()

Dim OutApp As Outlook.Application
Dim Namespace As Outlook.Namespace
Dim Mfolder As Outlook.MAPIFolder
Dim myMail As Outlook.Items

Dim Folder As Outlook.MAPIFolder
Dim SubFolder As Outlook.MAPIFolder
Dim UserFolder As Outlook.MAPIFolder

Set OutApp = New Outlook.Application
Set Namespace = OutApp.GetNamespace("MAPI")

On Error Resume Next
For Each Folder In Namespace.Folders
    For Each SubFolder In Folder.Folders
        For Each UserFolder In SubFolder.Folders
            Debug.Print Folder.Name, "|", SubFolder.Name, "|", UserFolder.Name
        Next UserFolder
    Next SubFolder
Next Folder
On Error GoTo 0

End Sub

The on error is to skip any issues with outlook mapping Archive pst files.

错误是跳过 Outlook 映射存档 pst 文件的任何问题。