# Outlook 2010 VBA question



## Biking Loki (May 22, 2013)

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


----------



## Norie (May 22, 2013)

Set the item's Unread property to False before moving them.


----------



## Biking Loki (May 22, 2013)

Norie said:


> Set the item's Unread property to False before moving them.



Thanks for the reply. I am still learning when it comes to VBA. How do I do that?


----------



## Norie (May 22, 2013)

Like this, which needs to go _before _you move the item.

```
objItem.Unread = False
```


----------



## Biking Loki (May 22, 2013)

Norie said:


> Like this, which needs to go _before _you move the item.
> 
> ```
> objItem.Unread = False
> ```



I've tried it in a couple different lines....where exactly would I insert it in the code listed above?


----------



## Norie (May 22, 2013)

Just before moving to the other folder.

```
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
```


----------



## Biking Loki (May 23, 2013)

Norie said:


> Just before moving to the other folder.
> 
> ```
> For Each objItem In Application.ActiveExplorer.Selection
> ...



Works exactly how I hoped it would. Thanks for your replies and patience.


----------



## Biking Loki (May 29, 2013)

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?


----------



## Biking Loki (May 29, 2013)

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?


----------



## Norie (May 29, 2013)

Have you tried deleting the item?

```
If objItem.Class = olMail Then
                objItem.Delete
                
            End If
```


----------



## Biking Loki (May 22, 2013)

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


----------



## Biking Loki (May 31, 2013)

Norie said:


> Have you tried deleting the item?
> 
> ```
> If objItem.Class = olMail Then
> ...




It took a little trial and error but this is what I came up with and it works. Thank you!


```
Sub MarkUnreadDelete()
On Error Resume Next

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

Set ns = Application.GetNamespace("MAPI")
 
For Each objItem In Application.ActiveExplorer.Selection
        If objItem.Class = olMail Then

                objItem.UnRead = False

                objItem.Delete
        End If
    Next
End Sub
```


----------

