Excel vba:循环浏览 Outlook 电子邮件中的所有子文件夹以查找具有特定主题的电子邮件 [英] Excel vba: Looping through all subfolders in Outlook email to find an email with certain subject

查看:95
本文介绍了Excel vba:循环浏览 Outlook 电子邮件中的所有子文件夹以查找具有特定主题的电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

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

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?

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

推荐答案

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

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.

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

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

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

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

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

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

这篇关于Excel vba:循环浏览 Outlook 电子邮件中的所有子文件夹以查找具有特定主题的电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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