扫描电子邮件的非默认Outlook收件箱?

我正在使用以下vba代码,检查具有特定主题标题的任何电子邮件。

问题是它检查我的默认outlook收件箱文件夹,当我需要它来检查我的其他电子邮件帐户的收件箱NewSuppliers@Hewden.co.uk

有人可以告诉我如何做到这一点? 提前致谢

Sub Macro1() Set olApp = CreateObject("Outlook.Application") Dim olNs As Outlook.Namespace Dim Fldr As Outlook.MAPIFolder Dim myItem As Outlook.MailItem Dim myAttachment As Outlook.Attachment Dim I As Long Dim olMail As Variant Set olApp = New Outlook.Application Set olNs = olApp.GetNamespace("MAPI") Set Fldr = olNs.GetDefaultFolder(olFolderInbox) Set myTasks = Fldr.Items Set olMail = myTasks.Find("[Subject] = ""New Supplier Request: Ticket""") If Not (olMail Is Nothing) Then For Each myItem In myTasks If myItem.Attachments.Count <> 0 Then For Each myAttachment In myItem.Attachments If InStr(myAttachment.DisplayName, ".txt") Then I = I + 1 myAttachment.SaveAsFile "\\uksh000-file06\Purchasing\NS\Unactioned\" & myAttachment End If Next End If Next For Each myItem In myTasks myItem.Delete Next Call Macro2 Else MsgBox "There Are No New Supplier Requests." End If End Sub 

outlook文件夹结构:

 account1@hewden.co.uk Inbox Drafts Sent NewSuppliers@hewden.co.uk Inbox Drafts Sent 

假设您需要的文件夹在文件夹层次结构中处于同一级别,则需要使用以下内容

 Set Items = Session.GetDefaultFolder(olFolderCalendar).Parent.Folders("YouFolderName").Items 

在这里看到更多的细节… http://www.slipstick.com/developer/working-vba-nondefault-outlook-folders/


你有没有尝试从以上链接中的以下function…

 Function GetFolderPath(ByVal FolderPath As String) As Outlook.Folder Dim oFolder As Outlook.Folder Dim FoldersArray As Variant Dim i As Integer On Error GoTo GetFolderPath_Error If Left(FolderPath, 2) = "\\" Then FolderPath = Right(FolderPath, Len(FolderPath) - 2) End If 'Convert folderpath to array FoldersArray = Split(FolderPath, "\") Set oFolder = Application.Session.Folders.item(FoldersArray(0)) If Not oFolder Is Nothing Then For i = 1 To UBound(FoldersArray, 1) Dim SubFolders As Outlook.Folders Set SubFolders = oFolder.Folders Set oFolder = SubFolders.item(FoldersArray(i)) If oFolder Is Nothing Then Set GetFolderPath = Nothing End If Next End If 'Return the oFolder Set GetFolderPath = oFolder Exit Function GetFolderPath_Error: Set GetFolderPath = Nothing Exit Function End Function 

您可能首先需要使用此技术来查找实际的文件夹名称… https://msdn.microsoft.com/en-us/library/office/ff184607.aspx


在下面的图片中,“草稿箱”,“客户端”,“发件箱”文件夹都处于同一级别(它们共享相同的父文件夹james @ … com),但ChildFolder文件夹不是(它的父级是草稿箱)。

在这里输入图像描述

您应该能够将第二个邮箱的名称指定为文件夹。

 Set Fldr = olNs.Folders("My 2nd mailbox").Folders("Sub Folder") 

如果您需要第二个帐户的主收件箱,则将其指定为该帐户的子文件夹。

 Set Fldr = olNs.Folders("My 2nd Inbox").Folders("Inbox") 

请注意,这是您使用的邮箱的名称,而不是电子邮件地址。 让我知道你是怎么办的。