Scan a non-default mailbox for email?

I am using the following vba code which checks any emails with a specific subject title.

The problem is that it checks my default inbox when I need to, to check the inbox of another email account NewSuppliers@Hewden.co.uk

Can someone please show me how I would do this? thanks in advance

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

      

Folder structure

outlook:

account1@hewden.co.uk
Inbox
Drafts
Sent

NewSuppliers@hewden.co.uk
Inbox
Drafts
Sent

      

+3


source to share


2 answers


You need to use the following, assuming that the desired folder is at the same level in the folder hierarchy

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

      

See here for details http://www.slipstick.com/developer/working-vba-nondefault-outlook-folders/


Have you tried the following function from the link above ...



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

      

You may need to use the technique first to find out the actual folder name ... https://msdn.microsoft.com/en-us/library/office/ff184607.aspx


In the figure below, the Drafts, Clients, and Outbox folders are at the same level (they have the same parent folder james @ ... com), but the ChildFolder folder is not (it is the parent of Drafts).

enter image description here

0


source


You just have to specify the name of your second mailbox as a folder.

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

      

If you want to use the primary mailbox for the second account, specify this as an account subfolder.



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

      

Please note that this is the name of the mailbox you are using, not your email address. Let me know how you are doing.

0


source







All Articles