VBA to move emails from one folder to archive folder

gittymoe

Board Regular
Joined
Apr 23, 2005
Messages
79
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)


VBA Code:
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
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
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...

VBA Code:
 Dim objItem As Variant

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

VBA Code:
    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!
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,223,246
Messages
6,170,999
Members
452,373
Latest member
TimReeks

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top