Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I select an archive folder?

I have an email account "[email protected]" (domain being made up).

Outlook shows an archive named " Archive - [email protected]" where Outlook automatically moves emails after a certain period.

Current code:

Set olRecip = olNS.CreateRecipient("Archive - [email protected]")
olRecip.Resolve
Set olFolder = olNS.GetSharedDefaultFolder(olRecip, olFolderInbox)

This opens the main inbox. How do I select the archive folder?

like image 752
Martin Washbrook Avatar asked Jan 18 '18 12:01

Martin Washbrook


2 Answers

"Archive" folder is usually at the root level - like inbox in that case:

Sub ArchiveItems()
' Moves each of the selected items on the screen to an Archive folder.
  Dim olApp As New Outlook.Application
  Dim olExp As Outlook.Explorer
  Dim olSel As Outlook.Selection
  Dim olNameSpace As Outlook.NameSpace
  Dim olArchive As Outlook.Folder
  Dim intItem As Integer
  Set olExp = olApp.ActiveExplorer
  Set olSel = olExp.Selection
  Set olNameSpace = olApp.GetNamespace("MAPI")

  Set olArchive = olNameSpace.Folders("[email protected]").Folders("Archive")

  For intItem = 1 To olSel.Count
      olSel.Item(intItem).Move olArchive
  Next intItem
End Sub

to get Inbox you could use default access:

Dim olInbox As Outlook.Folder
Set olInbox = olNameSpace.GetDefaultFolder(olFolderInbox)

Note - This will get you the default Inbox folder, if you have a few accounts in outlook you should verify it's really the folder you want - or use the mail specific approach like in Archive folder above

For Debugging - if you want to check all available subfolders

For i = 1 To olInbox.Folders.Count
    Debug.Print olInbox.Folders(i).Name
Next i
like image 170
Jaakov Jalink Avatar answered Sep 21 '22 13:09

Jaakov Jalink


Should be

Dim ARCHIVE_FOLDER As Outlook.MAPIFolder
Set ARCHIVE_FOLDER = olNs.Folders("Archive - [email protected]")

Full Example

Option Explicit
Public Sub Example()
    Dim olNs As Outlook.NameSpace
    Dim ARCHIVE_FOLDER As Outlook.MAPIFolder
    Dim Items As Outlook.Items
    Dim i As Long

    Set olNs = Application.Session
    Dim ARCHIVE_FOLDER As Outlook.MAPIFolder
    Set ARCHIVE_FOLDER = olNs.Folders("Archive - [email protected]") _
                             .Folders("Inbox")

    Debug.Print ARCHIVE_FOLDER.Name
    Debug.Print ARCHIVE_FOLDER.FolderPath
    Debug.Print ARCHIVE_FOLDER.Store.DisplayName

    ARCHIVE_FOLDER.Display

    Set Items = ARCHIVE_FOLDER.Items
    For i = Items.Count To 1 Step -1
        DoEvents
        Debug.Print Items(i).Subject
    Next

End Sub

MAPIFolder Object

like image 35
0m3r Avatar answered Sep 20 '22 13:09

0m3r