Link to home
Create AccountLog in
Avatar of bsharath
bsharathFlag for India

asked on

Cut paste multiple folders in Outlook. Any script that can help moving multiple folders in one shot.

Hi,

Cut paste multiple folders in Outlook. Any script that can help moving multiple folders in one shot.
I have a very large no of folders in my outlook in multiple pst's . Now when i need to move (Cut) and paste a few folders from one pst to another .It becomes very difficult. is there any way to select multiple folders and right click cut and paste in the desired path.

Regards
Sharath
Avatar of David Lee
David Lee
Flag of United States of America image

Sharath,

I have a solution for this.  That solution comes in three parts.  This is part 1 of 3.


Follow these instructions to use this.

1.  Start Outlook
2.  Click Tools > Macro > Visual Basic Editor
3.  If not already expanded, expand Microsoft Office Outlook Objects
4.  Right-click on Class Modules, select Insert > Class Module
5.  In the Properties panel click on Name and enter MultiFolderMover
6.  Copy the code from the Code Snippet box and paste it into the right-hand pane of Outlook's VB Editor window
7.  Edit the code as needed.  I included comments wherever something needs to or can change
8.  Click the diskette icon on the toolbar to save the changes
9.  Close the VB Editor


Const CLASSNAME = "Multiple Folder Mover"
Public olkRoot As Outlook.Folder
Private objFolders As Collection, olkDest As Outlook.Folder
 
Private Sub Class_Initialize()
    Set olkRoot = Outlook.Application.ActiveExplorer.CurrentFolder
    Set objFolders = New Collection
End Sub
 
Private Sub Class_Terminate()
    Set olkRoot = Nothing
    Set objFolders = Nothing
End Sub
 
Public Sub Move()
    Dim olkFolder As Outlook.Folder, varName As Variant
    If (objFolders.Count > 0) And (TypeName(olkDest) <> "Nothing") Then
        If MsgBox("Are you sure you want to move " & objFolders.Count & " folders to " & olkDest.Name & "?", vbQuestion + vbYesNo, CLASSNAME) = vbYes Then
            For Each varName In objFolders
                Set olkFolder = OpenOutlookFolder(olkRoot.FolderPath & "\" & CStr(varName))
                olkFolder.MoveTo olkDest
            Next
        Else
            MsgBox "Move aborted by user.", vbInformation + vbOKOnly, CLASSNAME
        End If
    Else
        MsgBox "Either or both the source and destination folders have not been selected.  Move aborted.", vbCritical + vbOKOnly, CLASSNAME
    End If
End Sub
 
Public Sub SelectDestination()
    Set olkDest = Outlook.Application.Session.PickFolder()
End Sub
 
Public Sub SelectSource()
    Dim objDialog As New frmFolderChooser, _
        intIndex As Integer
    objDialog.Show
    If objDialog.bolSelected Then
        For intIndex = 0 To objDialog.lbFolders.ListCount - 1
            If objDialog.lbFolders.Selected(intIndex) Then
                objFolders.Add olkRoot.Folders(objDialog.lbFolders.List(intIndex))
            End If
        Next
    Else
        MsgBox "You did not select any folders.", vbCritical + vbOKOnly, CLASSNAME
    End If
    Unload objDialog
End Sub
 
Function IsNothing(obj)
  If TypeName(obj) = "Nothing" Then
    IsNothing = True
  Else
    IsNothing = False
  End If
End Function
 
Function OpenOutlookFolder(strFolderPath As String) As Outlook.MAPIFolder
    Dim arrFolders As Variant, _
        varFolder As Variant, _
        olkFolder As Outlook.MAPIFolder
    On Error GoTo ehOpenOutlookFolder
    If strFolderPath = "" Then
        Set OpenOutlookFolder = Nothing
    Else
        Do While Left(strFolderPath, 1) = "\"
            strFolderPath = Right(strFolderPath, Len(strFolderPath) - 1)
        Loop
        arrFolders = Split(strFolderPath, "\")
        For Each varFolder In arrFolders
            If IsNothing(olkFolder) Then
                Set olkFolder = Session.Folders(varFolder)
            Else
                Set olkFolder = olkFolder.Folders(varFolder)
            End If
        Next
        Set OpenOutlookFolder = olkFolder
    End If
    On Error GoTo 0
    Exit Function
ehOpenOutlookFolder:
    Set OpenOutlookFolder = Nothing
    On Error GoTo 0
End Function

Open in new window

This is part 2 of 3.

Follow these instructions for this part.

1.  Start Outlook
2.  Click Tools > Macro > Visual Basic Editor
3.  If not already expanded, expand Microsoft Office Outlook Objects and click on Module1
4.  Copy the code from the Code Snippet box and paste it into the right-hand pane of Outlook's VB Editor window
5.  Edit the code as needed.  I included comments wherever something needs to or can change
6.  Click the diskette icon on the toolbar to save the changes
7.  Close the VB Editor


Sub MoveMultipleFolders()
    Dim objMFM As New MultiFolderMover
    With objMFM
        .SelectSource
        .SelectDestination
        .Move
    End With
    Set objMFM = Nothing
End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of David Lee
David Lee
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of bsharath

ASKER

Thanks a lot David...

I get this
---------------------------
Microsoft Visual Basic
---------------------------
Compile error:

Expected user-defined type, not project
---------------------------
OK   Help  
---------------------------
Thanks a lot David...

I get this
---------------------------
Microsoft Visual Basic
---------------------------
Compile error:

Expected user-defined type, not project
---------------------------
OK   Help  
---------------------------
Where does the error occur?  
When i run the macro i get the above error and this line shows as selected.

Dim objMFM As New MultiFolderMover
Did you follow all of the instructions in part 1 exactly?  If so, the you should have a class module named MultiFolderMover.  The error suggests that there is no such class module.
Ok i could not name the class i get this
---------------------------
Microsoft Visual Basic
---------------------------
Name conflicts with existing module, project, or object library
---------------------------
OK   Help  
---------------------------

So i named the name as project name
I searched all but did not find a similar "MultiFolderMover"
Did you create a code module named MultiFolderMover?  If so, then that's the problem.
Thanks a lot David that was one Awesome Form... :-))
Thanks, and you're welcome.  I'll take a look at the other question.