Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 如何在收件箱和子文件夹中搜索Outlook邮件_Excel_Vba_Outlook - Fatal编程技术网

Excel 如何在收件箱和子文件夹中搜索Outlook邮件

Excel 如何在收件箱和子文件夹中搜索Outlook邮件,excel,vba,outlook,Excel,Vba,Outlook,我已经创建了一个宏,它接收最新邮件并发送所有回复 现在我如何搜索收件箱和子文件夹并选择最新的 我的代码只从收件箱中选取邮件 Option Explicit Public Sub TESTRUN() Dim olApp As Outlook.Application Set olApp = New Outlook.Application Dim olNs As Outlook.Namespace Set olNs = olApp.GetNamespace("MAPI") Dim Inbox A

我已经创建了一个宏,它接收最新邮件并发送所有回复

现在我如何搜索收件箱和子文件夹并选择最新的

我的代码只从收件箱中选取邮件

Option Explicit
Public Sub TESTRUN()
Dim olApp As Outlook.Application
Set olApp = New Outlook.Application

Dim olNs As Outlook.Namespace
Set olNs = olApp.GetNamespace("MAPI")

Dim Inbox  As Outlook.MAPIFolder
Set Inbox = olNs.GetDefaultFolder(olFolderInbox)

Dim Subject As String
    Subject = ThisWorkbook.Sheets("SendMail").Range("B5").Text
    Debug.Print Subject

    Dim fpath As String
    fpath = ThisWorkbook.Sheets("SendMail").Range("A8").Value

Dim i As Long
Dim Filter As String
    Filter = "@SQL=" & Chr(34) & "urn:schemas:httpmail:datereceived" & _
                       Chr(34) & " >= '01/01/1900' And " & _
                       Chr(34) & "urn:schemas:httpmail:datereceived" & _
                       Chr(34) & " < '12/31/2100' And " & _
                       Chr(34) & "urn:schemas:httpmail:subject" & _
                       Chr(34) & "Like '%" & Subject & "%'"

Dim Items As Outlook.Items
Set Items = Inbox.Items.Restrict(Filter)
    Items.Sort "[ReceivedTime]", False

For i = Items.Count To 1 Step -1
    DoEvents
    If TypeOf Items(i) Is MailItem Then
        Dim Item As Object
        Set Item = Items(i)
        Debug.Print Item.Subject ' Print on Immediate Window
        Debug.Print Item.ReceivedTime ' Print on Immediate Window

        Dim ReplyAll As Outlook.MailItem
        Set ReplyAll = Item.ReplyAll

        With ReplyAll
             .Subject = Left(ActiveWorkbook.Name, InStr(ActiveWorkbook.Name, ".") - 1)
            .HTMLBody = "<font size=""3"" face=""Calibri"">" & _
              "Hi Veronica, <br><br>" & _
              "The " & Left(ActiveWorkbook.Name, _
                      InStr(ActiveWorkbook.Name, ".") - 1) & _
              "</B> has been prepared and ready for your review.<br>" & _
              "</B> <br>" & _
              "<A HREF=""file://" & fpath & """>" & fpath & "</A>" & .HTMLBody

            .Display
            Exit For

        End With

    End If
Next

End Sub
选项显式
公共子测试运行()
Dim olApp作为Outlook.Application
Set olApp=newoutlook.Application
将OLN设置为Outlook.Namespace
Set olNs=olApp.GetNamespace(“MAPI”)
将收件箱暗显为Outlook.Mapi文件夹
设置收件箱=olNs.GetDefaultFolder(olFolderInbox)
模糊主题为字符串
主题=此工作簿。工作表(“SendMail”)。范围(“B5”)。文本
调试。打印主题
作为字符串的Dim fpath
fpath=ThisWorkbook.Sheets(“SendMail”).Range(“A8”).Value
我想我会坚持多久
将筛选器设置为字符串
Filter=“@SQL=“&Chr(34)”和“urn:schemas:httpmail:datereceived”和_
Chr(34)和“>='01/01/1900'和“&_
Chr(34)&“urn:schemas:httpmail:datereceived”&_
Chr(34)和“<'12/31/2100'和”&_
Chr(34)和“urn:schemas:httpmail:subject”以及_
Chr(34)&类似“%”和Subject&“%”
将项目设置为Outlook。项目
设置项目=收件箱.Items.Restrict(过滤器)
Items.Sort“[ReceivedTime]”,False
对于i=项目。计数为1步骤-1
多芬特
如果项目类型(i)为MailItem,则
将项目变暗为对象
集合项目=项目(i)
调试.打印项.主题'在即时窗口上打印
Debug.Print Item.ReceivedTime“在即时窗口上打印”
Dim ReplyAll作为Outlook.MailItem
Set replyll=Item.replyll
答复
.Subject=Left(ActiveWorkbook.Name,InStr(ActiveWorkbook.Name,“.”)1)
.HTMLBody=”“&_
“嗨,维罗妮卡,

”和_ “The”&左(ActiveWorkbook.Name_ InStr(ActiveWorkbook.Name,“.”-1)和_ “已准备好并准备好供您审阅。
”&_ “
”和_ “”&.HTMLBody .展示 退出 以 如果结束 下一个 端接头
您可以从收件箱开始转换代码递归函数:示例

选项显式
公共子示例()
将olNs设置为Outlook.NameSpace
将收件箱暗显为Outlook.Mapi文件夹
Set olNs=Application.GetNamespace(“MAPI”)
设置收件箱=olNs.GetDefaultFolder(olFolderInbox)
“//处理当前文件夹
循环文件夹收件箱
设置收件箱=无
端接头
专用函数LoopFolders(ByVal ParentFldr作为Outlook.MAPIFolder)
模糊主题为字符串
主题=此工作簿。工作表(“SendMail”)。范围(“B5”)。文本
作为字符串的Dim FPath
FPath=ThisWorkbook.Sheets(“SendMail”).Range(“A8”).Value
将筛选器设置为字符串
Filter=“@SQL=“&Chr(34)”和“urn:schemas:httpmail:datereceived”和_
Chr(34)和“>='01/01/1900'和“&_
Chr(34)&“urn:schemas:httpmail:datereceived”&_
Chr(34)和“<'12/31/2100'和”&_
Chr(34)和“urn:schemas:httpmail:subject”以及_
Chr(34)&类似“%”和Subject&“%”
将项目设置为Outlook。项目
Set Items=ParentFldr.Items.Restrict(过滤器)
Items.Sort“[ReceivedTime]”,False
我想我会坚持多久
对于i=项目。计数为1步骤-1
多芬特
如果项目类型(i)为MailItem,则
将项目变暗为对象
集合项目=项目(i)
Debug.Print Item.Subject&&Item.ReceivedTime
Dim ReplyAll作为Outlook.MailItem
Set replyll=Item.replyll
答复
.Subject=“”
.HTMLBody=”“'
.展示
以
退出功能
如果结束
下一个
将子文件夹作为Outlook.MAPIFolder进行调整
“//通过子LDR递归
如果ParentFldr.Folders.Count>0,则
对于ParentFldr.文件夹中的每个子文件夹
循环文件夹子文件夹
Debug.Print SubFldr.Name
下一个
如果结束
端函数

fpath的可能重复项是excel文件位置,我将其用于超链接文件
Option Explicit
Public Sub Example()
    Dim olNs As Outlook.NameSpace
    Dim Inbox As Outlook.MAPIFolder

    Set olNs = Application.GetNamespace("MAPI")
    Set Inbox = olNs.GetDefaultFolder(olFolderInbox)

'   // Process Current Folder
    LoopFolders Inbox

    Set Inbox = Nothing
End Sub

Private Function LoopFolders(ByVal ParentFldr As Outlook.MAPIFolder)

    Dim Subject As String
        Subject = ThisWorkbook.Sheets("SendMail").Range("B5").Text

    Dim FPath As String
        FPath = ThisWorkbook.Sheets("SendMail").Range("A8").Value

    Dim Filter As String
        Filter = "@SQL=" & Chr(34) & "urn:schemas:httpmail:datereceived" & _
                           Chr(34) & " >= '01/01/1900' And " & _
                           Chr(34) & "urn:schemas:httpmail:datereceived" & _
                           Chr(34) & " < '12/31/2100' And " & _
                           Chr(34) & "urn:schemas:httpmail:subject" & _
                           Chr(34) & "Like '%" & Subject & "%'"

    Dim Items As Outlook.Items
    Set Items = ParentFldr.Items.Restrict(Filter)
        Items.Sort "[ReceivedTime]", False

    Dim i As Long
    For i = Items.Count To 1 Step -1
        DoEvents
        If TypeOf Items(i) Is MailItem Then
            Dim Item As Object
            Set Item = Items(i)

            Debug.Print Item.Subject & " " & Item.ReceivedTime

            Dim ReplyAll As Outlook.MailItem
            Set ReplyAll = Item.ReplyAll

            With ReplyAll
                 .Subject = ""
                 .HTMLBody = "" '
                 .Display
            End With
             Exit Function
        End If
    Next

    Dim SubFldr As Outlook.MAPIFolder
'   // Recurse through SubFldrs
    If ParentFldr.Folders.Count > 0 Then
        For Each SubFldr In ParentFldr.Folders
            LoopFolders SubFldr
            Debug.Print SubFldr.Name
        Next
    End If

End Function