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

查看:110
本文介绍了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中的所有文件夹,直至收件箱下一级.您可以通过指定要查看的初始文件夹来查看收件箱.因此,您可以在循环浏览时搜索该文件夹.您可以通过更深入地循环或说出folder.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令人沮丧,因此使此Early Bound变得更容易编码.这意味着您将需要转到工具/参考"并添加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

您可以在编码后将其更改回后期绑定,因为早期绑定将为您提供IntelliSense并大大简化了生活.

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