4
votes

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

The problem is it checks my default outlook inbox folder when I need it to check the inbox of my other email account [email protected]

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

outlook folder structure:

[email protected]
Inbox
Drafts
Sent

[email protected]
Inbox
Drafts
Sent
2
I've updated my answer to explain what i mean by 'same level' ... Also ... what version of Outlook are you using?3-14159265358979323846264
Is that an Exchange mailbox? Is it already open in Outlook?Dmitry Streblechenko

2 Answers

0
votes

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

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

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


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

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 here first to find out the actual folder name ... https://msdn.microsoft.com/en-us/library/office/ff184607.aspx


In the image below, the Drafts, Clients, Outbox folders are all on the same level (they share the same parent folder [email protected]), but the ChildFolder folder is not (it's parent is Drafts).

enter image description here

0
votes

You should be able to just specify the name of your 2nd mailbox as a Folder.

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

If you want the main inbox of the 2nd account, then you specify this as the sub folder of the account.

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

Note that it's the name of the mailbox you use, not the e-mail address. Let me know how you get on.