Outlook 2010 VBA question

Biking Loki

Board Regular
Joined
Aug 25, 2005
Messages
167
I found and modified the following code to make a 1 click macro that would move the selected email(s) to a folder designated in the macro. It works perfectly but I am wondering if there is a way to add the ability to also mark it as read. Is that possible?

'Outlook VB Macro to move selected mail item(s) to a target folder
Sub MoveToFiled()
On Error Resume Next

Dim ns As Outlook.NameSpace
Dim moveToFolder As Outlook.MAPIFolder
Dim objItem As Outlook.MailItem

Set ns = Application.GetNamespace("MAPI")

'Define path to the target folder
Set moveToFolder = ns.Folders("XXXX@XXX.com").Folders("Inbox").Folders("ALL_MAIL")

If Application.ActiveExplorer.Selection.Count = 0 Then
MsgBox ("No item selected")
Exit Sub
End If

If moveToFolder Is Nothing Then
MsgBox "Target folder not found!", vbOKOnly + vbExclamation, "Move Macro Error"
End If

For Each objItem In Application.ActiveExplorer.Selection
If moveToFolder.DefaultItemType = olMailItem Then
If objItem.Class = olMail Then
objItem.Move moveToFolder
End If
End If
Next

Set objItem = Nothing
Set moveToFolder = Nothing
Set ns = Nothing

End Sub
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Set the item's Unread property to False before moving them.
 
Upvote 0
Like this, which needs to go before you move the item.
Code:
objItem.Unread = False
 
Upvote 0
Just before moving to the other folder.
Code:
    For Each objItem In Application.ActiveExplorer.Selection
        If moveToFolder.DefaultItemType = olMailItem Then
            If objItem.Class = olMail Then

                objItem.UnRead = False   ' here

                objItem.Move moveToFolder



            End If
        End If
    Next
 
Upvote 0
Just before moving to the other folder.
Code:
    For Each objItem In Application.ActiveExplorer.Selection
        If moveToFolder.DefaultItemType = olMailItem Then
            If objItem.Class = olMail Then

                objItem.UnRead = False   ' here

                objItem.Move moveToFolder



            End If
        End If
    Next

Works exactly how I hoped it would. Thanks for your replies and patience.
 
Upvote 0
I am trying to modify this, as a sepereate macro, to do the same thing except to move it to the deleted items folder. I tried changing

Set moveToFolder = ns.Folders("XXXX@XXX.com").Folders("Inbox").Folders("ALL_MAIL")

to

Set moveToFolder = ns.Folders("XXXX@XXX.com").Folders("Deleted Items")

but it didn't work. Does that folder behave differently than a normal folder and as such need to be coded differently?
 
Upvote 0
I am trying to modify this, as a sepereate macro, to do the same thing except to move it to the deleted items folder. I tried changing

Set moveToFolder = ns.Folders("XXXX@XXX.com").Folders("Inbox").Folders("ALL_MAIL")

to

Set moveToFolder = ns.Folders("XXXX@XXX.com").Folders("Deleted Items")

but it didn't work. Does that folder behave differently than a normal folder and as such need to be coded differently?
 
Upvote 0
Have you tried deleting the item?
Code:
         If objItem.Class = olMail Then
                objItem.Delete
                
            End If
 
Upvote 0

Forum statistics

Threads
1,225,644
Messages
6,186,151
Members
453,339
Latest member
Stu61

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