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

时间:2015-07-15 08:03:23

标签: excel vba email 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

2 个答案:

答案 0 :(得分:0)

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

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

在下图中,草稿,客户端,发件箱文件夹都在同一级别(它们共享相同的父文件夹james @ ... com),但ChildFolder文件夹不是(它的父级是草稿)。 / p>

enter image description here

答案 1 :(得分:0)

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

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

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

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

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