Question

I am trying to copy a structure of folders and subfolders from one Outlook PST to another one and have difficulties with the Folders.Add() statement:

Private Sub Process(S As MAPIFolder, T As MAPIFolder, RootLevel As Boolean, BeforeDate As Date)
Dim N As NameSpace, F As MAPIFolder, G As MAPIFolder

    ' S is source folder (parameter)
    ' T is target folder (parameter)
    ' F is current source subfolder for recursion (private)
    ' G is target folder for recursion (private)

    Set N = Application.GetNamespace("MAPI")

    ' recurse through subfolders
    For Each F In S.Folders
        If F.Items.Count <> 0 Or F.Folders.Count <> 0 Then          ' process only if items or subfolders found

            If FoundFolder(T, F) Then                               ' this function works fine
                Set G = T.Folders(F.Name)                           ' found - just assign
            Else
                Set G = T.Folders.Add(F.Name, N.GetDefaultFolder(F.DefaultItemType))        ' not found - create
            End If
            '
            ' more code (working well)
            '                
            ' process next level without Root flag
            Process F, G, False, BeforeDate
        End If
    Next F
    Set F = Nothing
    Set G = Nothing
End Sub

As long as in the Folders.Add() statement I don't specify the Type parameter at all, a folder with DefaultType olMailItem is created (because my root folder happens to be a mail folder). However, I want to create a folder of same type as the source folder.

1st peculiar observation:

  • VBA Help, MSN and others say that for Folders.Add(Name, Type) Type is Optional Long.
  • VBA Editor says (in tooltip when typing) Type is MAPIFolder

2nd observation: However I try to set the Type argument, I receive an error


Error -2147024809 (80070057)

Could not complete the operation. One or more parameter values are not valid


I tried the following

' Type as Long
Set G = T.Folders.Add(F.Name, 0)
Set G = T.Folders.Add(F.Name, olMailItem)
Set G = T.Folders.Add(F.Name, OlItemType.olMailItem)
Set G = T.Folders.Add(F.Name, F.DefaultItemType) ' this is what I actually want
' Type as MAPIFolder
Set G = T.Folders.Add(F.Name, F)
Set G = T.Folders.Add(F.Name, N.GetDefaultFolder(F.DefaultItemType))

Error - Error - Error

What to do to create a folder of the same type as the source folder F

Anyone help .... please

kind regards MikeD

Was it helpful?

Solution

OK solved .... problem was I simply used the wrong enumeration :-(

The function creating a MAPI folder of type similar to Source below a given Target looks like this:

Private Function CreateFolderOfType(Source As MAPIFolder, Target As MAPIFolder) As MAPIFolder
Dim F As MAPIFolder

    Set CreateFolderOfType = Nothing
    ' if source already exists below Target
    For Each F In Target.Folders
        If F.Name = Source.Name Then
            Set CreateFolderOfType = F
            Exit Function
        End If
    Next F

    Select Case Source.DefaultItemType
    Case olAppointmentItem
        Set CreateFolderOfType = Target.Folders.Add(Source.Name, olFolderCalendar)
    Case olContactItem, olDistributionListItem
        Set CreateFolderOfType = Target.Folders.Add(Source.Name, olFolderContacts)
    Case olJournalItem
        Set CreateFolderOfType = Target.Folders.Add(Source.Name, olFolderJournal)
    Case olMailItem, olPostItem
        Set CreateFolderOfType = Target.Folders.Add(Source.Name, olFolderInbox)
    Case olNoteItem
        Set CreateFolderOfType = Target.Folders.Add(Source.Name, olFolderNotes)
    Case olTaskItem
        Set CreateFolderOfType = Target.Folders.Add(Source.Name, olFolderTasks)
    Case Else
        Set CreateFolderOfType = Target.Folders.Add(Source.Name)
    End Select
End Function
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top