0

Afternoon All,

I have the following structure in my Outlook:

Mapi > peter*********com > !!!INCIDENTS > !APS PV >!Archive

enter image description here

I would like to place ALL subfolders in subfolder !APS PV into subfolder !Archive .

My code looks to loop through the subfolder and moves each. I have no issues moving the subfolders, it's loading all the subfolders into an array. No items are being returned.

    Dim olFolder As Outlook.MAPIFolder
    Dim SubFolder As Outlook.MAPIFolder
    Dim olNs As Outlook.NameSpace
    Dim Item As Object
    Dim lngCount As Long
    Dim Items As Outlook.Items

    On Error GoTo MsgErr
'    Set Inbox Reference
    Set olNs = Application.GetNamespace("MAPI")
    Set olFolder = olNs.GetDefaultFolder(olFolderInbox).Parent
    Set Items = olFolder.Items
    Set olFolder = olFolder.Folders("!!!INCIDENTS")
    Set Items = olFolder.Items
    Set olFolder = olFolder.Folders("!APS PV")
    Set Items = olFolder.Items

'   // Loop through the Items in the folder backwards
    For lngCount = Items.Count To 1 Step -1
        Set Item = Items(lngCount)

        Debug.Print Item.Subject

        If Item.Class = olMail Then
'           // Set SubFolder of Inbox
            Set SubFolder = olFolder.Folders("!Archive")
'           // Mark As Read
            Item.UnRead = False
'           // Move Mail Item to sub Folder
            Item.Move SubFolder
        End If
    Next lngCount

The Items.Count returns zero despite many subfolders existing.

Peter

braX
  • 11,506
  • 5
  • 20
  • 33
Peter Lucas
  • 1,979
  • 1
  • 16
  • 27
  • Seems like folders are not items and you have to loop through folders collection in the folder. See [Using visual basic to access subfolder in Inbox?](https://stackoverflow.com/a/8322523/9439330) – ComputerVersteher Sep 12 '19 at 05:59
  • Strange as olFolder.Items.Item does exist in the object hierarchy. I'm seeing an ewrror being raised at olFolder.Folders("!!!INCIDENTS") The attempted operation failed. An object could not be found" – Peter Lucas Sep 12 '19 at 06:24
  • Item and Items are Outlook data types. Trying to use them as private variables gets Outlook confused. Try `MyItem` and `MyItems` or `ItemCrnt` and `ItemsCrnt`. – Tony Dallimore Sep 12 '19 at 18:00
  • Is `Debug.Print olNs.GetDefaultFolder(olFolderInbox).Parent` what you think it is? – niton Sep 24 '19 at 01:10
  • Looks all in order: Mapi p********.com !!!INCIDENTS !APS PV Just not items being returned from Items.Count – Peter Lucas Sep 24 '19 at 23:13
  • You cannot get to the line with Items.Count if `olFolder.Folders("!!!INCIDENTS")` generates "The attempted operation failed. An object could not be found". – niton Sep 24 '19 at 23:28
  • What do you see with `Debug.Print olNs.GetDefaultFolder(olFolderInbox).Parent`? Use@niton so I note your response. – niton Sep 24 '19 at 23:29
  • Compile Error: Expected expressioin in the debug window – Peter Lucas Sep 24 '19 at 23:30
  • Not in the immediate pane. Place the line in the code just before `Set olFolder = olNs.GetDefaultFolder(olFolderInbox).Parent`. – niton Sep 24 '19 at 23:33
  • Set olNs = Application.GetNamespace("MAPI") Debug.Print olNs Debug.Print olNs.GetDefaultFolder(olFolderInbox).Parent shows – Peter Lucas Sep 24 '19 at 23:37
  • Mapi p***.****@****.com This look reasonable – Peter Lucas Sep 24 '19 at 23:37

1 Answers1

0

You can list all the folders to see which ones are available.

Option Explicit

Sub FoldersList()

    Dim olNs As NameSpace

    Dim olMailbox As Folder

    Set olNs = GetNamespace("MAPI")

    Debug.Print olNs.GetDefaultFolder(olFolderInbox).Parent
    Set olMailbox = olNs.GetDefaultFolder(olFolderInbox).Parent

    processFolder olMailbox

End Sub

Private Sub processFolder(ByVal oParent As Folder)

    Dim oFolder As Folder

    Debug.Print oParent

    If oParent = "!!!INCIDENTS" Then MsgBox "found"

    If (oParent.Folders.Count > 0) Then
        For Each oFolder In oParent.Folders
            processFolder oFolder
        Next
    End If

End Sub
niton
  • 8,771
  • 21
  • 32
  • 52