Isendra
Isendra

Reputation: 57

Type Mismatch Error when referencing Folder with PickFolder

The following is supposed to launch a popup folder picker, and then move the current item to the selected folder.

 Sub MoveItems()
     Dim myNameSpace As Outlook.NameSpace
     Dim myInbox As Outlook.Folder
     Dim mySubFolder As Outlook.Folder
     Dim myDestFolder As Outlook.Folder
     Dim myItem As Object

     Set myNameSpace = Application.GetNamespace("MAPI")
     Set myInbox = myNameSpace.GetDefaultFolder(olFolderInbox)
     Set mySubFolder = myNameSpace.PickFolder
     Set myDestFolder = myInbox.Folders(mySubFolder)
     Set myItem = GetCurrentItem()

     myItem.Move myDestFolder

End Sub

I am getting a Type Mismatch on the line

Set myDestFolder = myInbox.Folders(mySubFolder)

Upvotes: 1

Views: 1000

Answers (1)

0m3r
0m3r

Reputation: 12507

That line should be Set myDestFolder = mySubFolder

You may wanna also use If mySubFolder Is Nothing Then Exit Sub just in case user decides to cancel the myNameSpace.PickFolder so you don't get run-time error

Option Explicit
Sub MoveItems()
     Dim myNameSpace As Outlook.NameSpace
     Dim myInbox As Outlook.Folder
     Dim mySubFolder As Outlook.Folder
     Dim myDestFolder As Outlook.Folder
     Dim myItem As Object

     Set myNameSpace = Application.GetNamespace("MAPI")
     Set myInbox = myNameSpace.GetDefaultFolder(olFolderInbox)
     Set mySubFolder = myNameSpace.PickFolder

    If mySubFolder Is Nothing Then Exit Sub

     Set myDestFolder = mySubFolder
     Set myItem = GetCurrentItem()

     myItem.Move myDestFolder

End Sub

Function GetCurrentItem() As Object
    Dim objApp As Outlook.Application

    Set objApp = Application
    On Error Resume Next
    Select Case TypeName(objApp.ActiveWindow)
        Case "Explorer"
            Set GetCurrentItem = objApp.ActiveExplorer.Selection.Item(1)
        Case "Inspector"
            Set GetCurrentItem = objApp.ActiveInspector.CurrentItem
    End Select

    Set objApp = Nothing
End Function

IsNothing Function

IsNothing returns True if the expression represents an object variable that currently has no object assigned to it; otherwise, it returns False.

Upvotes: 1

Related Questions