Leon Smith
Leon Smith

Reputation: 45

Outlook 2007 Multi Select folders

First Time Question :D.

I have been writing a Marco for Outlook 2007 to let the user pick a folder and then with that folder move all the mail items out of it and move it to a personal Arhcive folder where using the selected folder name as the destination under a personal folder.

e.g.

User selects a randon folder with ther name 'Test'

The marco would be pre mapped to personal folder and then it would locate the sub folder with the same name and move the mail items.

My questions:

I Currently am using the '.PickFolder' Syntax to have the use select the folder, What i what to do is have a Multi Select folders form:

I understand this would only be at one level but that is all i require as the 200+ folders are at one level.

As the code is, It works brilliantly with one folder at a time but i wish to up the anti .

Thnak you.

Upvotes: 1

Views: 1243

Answers (1)

Jim
Jim

Reputation: 138

I've tackled the problem of getting a full list of folders (code below) but if you need more help with other parts please add a comment and I will expand upon my answer.

I didn't understand what you would do with a ComboBox. So for this example I have created a form and added a ListBox (called ListBox1). The code below will populate the listbox with the names of all the folders from a selected folder downwards. Please read the comments to see what else you can do (e.g. recurse through subfolders - I know that's not required in this case).

Please do let me know if you need more help or information.

Private Sub PopulateListBoxWithFolders()

    Dim objApp As Outlook.Application
    Dim objNamespace As Outlook.NameSpace
    Dim objFolder As Outlook.MAPIFolder

    ' Clear current contents of listbox
    ListBox1.Clear

    Set objApp = New Outlook.Application

    Set objNamespace = objApp.GetNamespace("MAPI")

    ' Allow user to select folder.
    ' Replace this with either objNamespace.GetDefaultFolder(...) or objNamespace.GetFolderFromID(...)
    ' to avoid the user having to select a folder
    Set objFolder = objNamespace.PickFolder

    ' See if the user cancelled or no folder found
    If Not objFolder Is Nothing Then
        ' Addition of true here recurses through all subfolders
        ProcessFolder objFolder ', True
    End If

End Sub

' Populates the ListBox with the folders. Optionally you can recurse all folders
Sub ProcessFolder(objStartFolder As Outlook.MAPIFolder, Optional blnRecurseSubFolders As Boolean = False, Optional strFolderPath As String = "")

    Dim objFolder As Outlook.MAPIFolder

    Dim i As Long

     ' Loop through the items in the current folder
    For i = 1 To objStartFolder.Folders.Count

        Set objFolder = objStartFolder.Folders(i)

        ' Populate the listbox
        ListBox1.AddItem ListBox1.Text + objFolder.FolderPath

        If blnRecurseSubFolders Then
            ' Recurse through subfolders
            ProcessFolder objFolder, True, strFolderPath + "\" + objFolder.FolderPath
        End If
    Next

End Sub

If you need code to identify the selected items in a multiselect ListBox here it is. To make the ListBox multiselect you should set the MultiSelect property in the form editor to 1 - fmMultiSelectMulti

Private Sub btnOK_Click()
    Dim i As Long

    ' Loop through all items in the listbox identifying those that are selected
    With ListBox1
        For i = 0 To .ListCount - 1
            If .Selected(i) Then
                ' Here goes the code to act on the selected item
                ' In the example below it outputs to the Immediate window
                Debug.Print .List(i)
            End If
        Next i
    End With

End Sub

Upvotes: 1

Related Questions