# VBA to move emails from one folder to archive folder



## gittymoe (Apr 15, 2021)

Guys, I have cobbled together the below code to move emails from one folder to an archive folder. It seems to be identifying the from folder and the move to folder but it is erroring out. Run-Time Error '91: Object variable or With block variable not set (in Red below)



```
Sub MoveDraftMail()

Dim objOutlook As Outlook.Application

Dim objNamespace As Outlook.NameSpace

Dim objSourceFolder As Outlook.MAPIFolder

Dim objDestFolder As Outlook.MAPIFolder

Dim objItem As MailItem

Dim Archive_Folder As Outlook.MAPIFolder


Set objOutlook = New Outlook.Application

Set objNamespace = objOutlook.GetNamespace("MAPI")

Set objSourceFolder = objNamespace.Folders(10).Folders("HouseBillofLadingReport")

Set Archive_Folder = objNamespace.Folders("Online Archive - My@email").Folders("Personal_Folders").Folders("2021").Folders("InBox")


[COLOR=rgb(184, 49, 47)]ObjItem.Move Archive_Folder[/COLOR]


Set objDestFolder = Nothing


End Sub
```


Any suggestions would be appreciated.

Thank You


----------



## Domenic (Apr 18, 2021)

That's because you haven't assigned `objItem` an item from your source folder.  To move all mail items from your source folder to your archive folder, first declare objItem as Variant instead as MailItem...


```
Dim objItem As Variant
```

Then loop through each item in your source folder as follows...


```
For Each objItem In objSourceFolder.Items
        If TypeName(objItem) = "MailItem" Then
            objItem.Move Archive_Folder
        End If
    Next objItem
```

Note that it checks whether the item is in fact a mail item before moving it to the archive folder.

Hope this helps!


----------



## pbangsoe (May 6, 2021)

Hi @Domenic, @gittymoe,

I am a newbie, so I hope you will excuse me.

I hope one of you will share the whole working code to me, please.

Regards to you both
Peter


----------

