0

So I have multiple inboxes that I need to manipulate mail from. I am trying to loop through them and find the necessary mailbox, and folder, to move mails out of. When I get to "For Each oAccount in Outlook..." it tells me "object required". I'm having understanding how to make it loop through the accounts. I would be so so appreciative if anyone can show me where I'm making the error in the code below.

Thanks!

Sub MoveEmail()
    Dim oOlAp As Object, oOlns As Object, oOlInb As Object
    Dim oOlItm As Object
    Dim Br, Spec As Folder
    Dim oOlAtch As Object
    Dim eSender As String, dtRecvd As String, dtSent As String, o0Acct1 As String, o0Acct2 As String
    Dim sSubj As String, sMsg As String
    Dim wb As Workbook, wb2 As Workbook
    Dim fso As FileSystemObject
    Dim FName, NewFileName As String
    Dim sn As String

    'Set objects

    '~~> Get Outlook instance
    o0Acct1 = "Me@abc"
    o0Acct2 = "AlsoMe@abc"
    Set oOlAp = GetObject(, "Outlook.application")
    Set oOlns = oOlAp.GetNamespace("MAPI")
    Set oOlInb = oOlns.GetDefaultFolder(olFolderInbox)
    Set Br = oOlInb.Folders("Folder1")
    Set Sp = oOlInb.Folders("Folder2")
    Set oOlItm = Br.Items

'=====================================================

For Each oAccount In oOutlook.Sessions.Accounts
    If oAccount = o0Acct1 Then

        Dim i As Integer
        For i = Br.Items.Count To 1 Step -1   'loop goes from last to first element
            sn = Br.Items(i).SenderName

            If sn = "Them@abcd" Then
                Set dest = Sp
                Br.Items(i).Move dest

            Else
            End If
        Next
    Else
    End If
Next

End Sub

' ===========================================================================

Okay, so I've solved it. Instead of trying to cycle through accounts, I cycled through folders in different namespaces. I am able to cycle through to the correct account and folder with the code below. Thanks!

Sub List_All_NameSpace_Folders()
    Dim myNS As Namespace
    Dim i As Integer
    Dim sn As String

    Set myNS = GetNamespace("MAPI")
    With myNS
        For Each Folder In myNS.Folders

            If Folder = "Email@abc" Then
                Set Br = Folder.Folders("Inbox").Folders("Folder1")
                Set Cl = Folder.Folders("Inbox").Folders("Folder1").Folders("Folder2")

                For i = Br.Items.Count To 1 Step -1   'loop goes from last to first element
                        sn = Br.Items(i).SenderName
                         If sn = "Email2@abc" Then
                            Set dest = Cl
                            Br.Items(i).Move dest

                        Else
                        End If
                Next

            Else
            End If
        Next Folder
    End With
End Sub
Rossle
  • 40
  • 5

2 Answers2

0

"Session" must singular, not plural:

For Each oAccount In oOutlook.Session.Accounts
Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
  • That didn't quite do it :/ – Rossle Jul 19 '18 at 22:44
  • Also, you need to compare the account display name: If oAccount.DisplayName = o0Acct1 Then – Dmitry Streblechenko Jul 19 '18 at 22:44
  • Run time error 424: Object required.. Could it be an issue missing a reference? Or did I perhaps not dim o0Account? – Rossle Jul 19 '18 at 22:46
  • I figured it out. I needed to add the reference library Microsoft Outlook 15.0 Object Library, and I needed to Dim oAccount as Outlook.Account. Second, I needed to remove the "o" in oOutlook.Application.Session.Accounts. The issue now, is that it only sees one account for some reason, while I have multiple; and it only makes one loop and ends – Rossle Jul 19 '18 at 23:09
  • What do you see in OutlookSpy if you click Namespace button, select Accounts property, click Browse, go to the EnumVariant tab? – Dmitry Streblechenko Jul 20 '18 at 16:00
  • Unfortunately I'm on an office computer and not able to download without clearance :( Is there an alternative to how I can see what's within namespace? – Rossle Jul 20 '18 at 16:57
  • You can run a test script like the one you have above, but it does not work for you too well. What does "MsgBox oOlAp.Session.Accounts.Count" show? – Dmitry Streblechenko Jul 20 '18 at 17:38
  • Oops, sorry. Having trouble formatting. But if you copy and past that above, it will cycle above all the FOLDERs in all inboxes. I'm still trying to figure out the difference between the code above and my previous code. – Rossle Jul 20 '18 at 18:13
  • It shows 1. Though I found an interesting work around with this bit of code strFolderList = "Your Outlook NameSpace contains these folders:" Set myNS = GetNamespace("MAPI") With myNS For Each Folder In myNS.Folders If Folder = "Email@abc" Then Set oOlInb = Folder.GetDefaultFolder(olFolderInbox) Set Br = myNS.Folders("Broker Paper") Set Sp = myNS.Folders("Cleared") Else End If Next Folder End With – Rossle Jul 20 '18 at 18:20
  • Okay, I've solved it! Thanks for your help, I put my final code in an edit above. I don't completely understand how it's working but at least it's working. – Rossle Jul 20 '18 at 19:18
0

As you indicated in a comment, you have one account, so you cannot change accounts.

In your working solution you find a folder named Email@abc that is one email address in your account.

Whether the Br folder is in the default inbox or not, you may reference the folder directly, the long way, without using .GetDefaultFolder.

Instead of cycling through folders:

Sub referenceOneOfManyEmailAddressesInSingleAccount()

    Dim myNS As Namespace
    Dim emFldr as folder
    Dim inbxFldr as folder
    Dim Br as folder
    Dim dest as folder

    Dim i As Long
    Dim sn As String

    Set myNS = GetNamespace("MAPI")
    Set emFldr = myNS.Folders("Email@abc")
    Set inbxFldr = emFldr.Folders("Inbox")

    Set Br = inbxFldr.Folders("Folder1")
    Set dest = Br.Folders("Folder2")

    For i = Br.Items.Count To 1 Step -1   'loop goes from last to first element
        sn = Br.Items(i).SenderName
        If sn = "Email2@abc" Then
            Br.Items(i).Move dest
        End If
    Next

End Sub
niton
  • 8,771
  • 21
  • 32
  • 52
  • So I have multiple other accounts, the others are shared by myself and my team. So I don't know what was wrong with the first code I posted that I couldn't cycle through them. But the second piece of code I edited in below the first works fine and lets me cycle through them. Once I cycled through I figured out how I could set BR directly equal to the relevant folder. – Rossle Jul 23 '18 at 16:51
  • Would you be able to explain what the GetNamespace function does? I don't quite understand what is happening when that is executed. What is the namespace or MAPI exactly? – Rossle Jul 23 '18 at 16:52