扫描非默认 Outlook 收件箱以查找电子邮件?
Posted
技术标签:
【中文标题】扫描非默认 Outlook 收件箱以查找电子邮件?【英文标题】:Scan non default outlook inbox for email? 【发布时间】:2015-10-04 03:52:05 【问题描述】:我正在使用以下 vba 代码检查具有特定主题标题的任何电子邮件。
问题是当我需要它来检查我的其他电子邮件帐户 NewSuppliers@Hewden.co.uk 的收件箱时,它会检查我的默认 Outlook 收件箱文件夹
有人可以告诉我如何做到这一点吗?提前致谢
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
【问题讨论】:
我已经更新了我的答案,以解释我所说的“相同级别”是什么意思......另外......你使用的是什么版本的 Outlook? 那是 Exchange 邮箱吗?是否已在 Outlook 中打开? 【参考方案1】:你需要使用以下,假设你想要的文件夹在文件夹层次结构中处于同一级别
Set Items = Session.GetDefaultFolder(olFolderCalendar).Parent.Folders("YouFolderName").Items
更多详情请看这里...http://www.slipstick.com/developer/working-vba-nondefault-outlook-folders/
您是否尝试过上述链接中的以下功能...
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
在下图中,Drafts、Clients、Outbox 文件夹都在同一级别(它们共享同一个父文件夹 james@...com),但 ChildFolder 文件夹不是(它的父文件夹是 Drafts)。
【讨论】:
谢谢,但这似乎不起作用,它说找不到对象 您可以发布您的文件夹结构的图像吗?此外,该链接处理文件夹处于不同级别的其他情况:0) 请查看更新后的问题与 Outlook 文件夹结构account1@hewden.co.uk
和 NewSuppliers@hewden.co.uk
是否处于同一级别?
同级是什么意思?【参考方案2】:
您应该能够将您的第二个邮箱的名称指定为文件夹。
Set Fldr = olNs.Folders("My 2nd mailbox").Folders("Sub Folder")
如果您想要第二个帐户的主收件箱,则将其指定为该帐户的子文件夹。
Set Fldr = olNs.Folders("My 2nd Inbox").Folders("Inbox")
请注意,这是您使用的邮箱名称,而不是电子邮件地址。让我知道你的进展情况。
【讨论】:
以上是关于扫描非默认 Outlook 收件箱以查找电子邮件?的主要内容,如果未能解决你的问题,请参考以下文章