PDA

View Full Version : VBA Move Emails from one folder to specific Folder



OGgittymoe
04-15-2021, 08:52 AM
Guys, I have cobbled together the below code to move emails from one folder to another. I attached my code and original message it won't let me paste here.


Thank you,

gmayor
04-15-2021, 09:01 PM
Assuming that you are running this macro fro Outlook, you don't need to create a new Outlook instance. Use the active one.
You haven't told the macro what objItem is.
Assuming your paths are correct the following should work and I see nothing wrong with the line in question


Sub MoveDraftMail()

Dim objNamespace As Outlook.NameSpace
Dim objSourceFolder As Outlook.MAPIFolder
Dim objDestFolder As Outlook.MAPIFolder
Dim objItem As Object
Dim Archive_Folder As Outlook.MAPIFolder


Set objNamespace = Application.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")
For Each objItem In objSourceFolder.items
If TypeName(objItem) = "MailItem" Then
objItem.Move Archive_Folder
End If
DoEvents
Next objItem
Set objDestFolder = Nothing
Set objNamespace = Bothing
Set objSourceFolder = Nothing
Set Archive_Folder = Nothing
Set objItem = Nothing
End Sub

OGgittymoe
04-16-2021, 05:16 AM
Gmayor....you nailed it. Thank you!:super: