Outlook VBA not moving emails

I am running the following code to move all emails with the category 'File' to another specified folder.  The issue I am experiencing is that when the code is executed, not all of the emails within the folder (inbox) are moved, even though the FOR loop is run through all the objects within the 'inbox' folder.

Once the code is executed several times, it does move the desired emails.  Any suggestions on why this is occurring?

Set objFolder = Outlook.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)

For Each objItems In objFolder.Items
  If (objItems.Class = olMail) Then
    If (objItems.Categories = "File") Then
      objItems.move objDest
    End If
  End If
Next

Open in new window

CmitchAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

bromy2004Commented:
In your macro, is there any setting changes made?
i.e it changes the Category to something else?
or removes the category?
0
CmitchAuthor Commented:
No changes are made, it simply moves the emails with category 'File' to another destination folder.
0
Chris BottomleySoftware Quality Lead EngineerCommented:
Hello Cmitch,

The issue is if you imagine there are three items, the loop processes 1 and when complete there are only two items in the folder.  It now processes the second and there is only 1 item in the folder ... where is the third item for it to process?

When deleting or moving items you need to work backwards i.e.:

Regards,

chris_bottomley
Set objFolder = Outlook.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)
dim itmCount as integer

For itmcount = objFolder.Items.count to 1 step -1
  set objitems = objFolder.Items(itmCount)
  If (objItems.Class = olMail) Then
    If (objItems.Categories = "File") Then
      objItems.move objDest
    End If
  End If
Next

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

bromy2004Commented:
Chris,
I was under the impression that he was using For Each, not For i = x to y,
You only need to Work backwards, If you are deleting AND using a count.
0
Chris BottomleySoftware Quality Lead EngineerCommented:
bromy2004:

Wrong i'm afraid, that is the fundamental issue with for each you think it's ok but it sure isn't.  i.e. as you move an item the collection reduces so the loop stops halpway through each time.

You should always do delete and move by a reverse count ... in my experience and since I do make mistakes I did a quick test with the following code and as I suggested only half the existing files move each time.

Chris


Sub copyeach()
Dim src As Object
Dim tgt As Object
Dim itm As Object

    Set src = Application.Session.GetDefaultFolder(olFolderInbox).folders("chris").folders("Test")
    Set tgt = Application.Session.GetDefaultFolder(olFolderInbox).folders("chris").folders("Test2")
    For Each itm In src.items
        itm.Move tgt
    Next
End Sub

Open in new window

0
CmitchAuthor Commented:
Thanks, certainly a lesson learnt with FOR EACH.
0
bromy2004Commented:
I use the Attached macro (Prior to 2010) and it always goes through every item.
unless its a failed delivery message ect.

should it not be working?
Sub MoveSelectedMessagesToFolder()

On Error Resume Next
  Dim objFolder As Outlook.MAPIFolder, objInbox As Outlook.MAPIFolder
  Dim objNS As Outlook.NameSpace, objItem As Outlook.MailItem
  Dim objSecFolder As Outlook.MAPIFolder

  Set objNS = Application.GetNamespace("MAPI")
  Set objInbox = objNS.GetDefaultFolder(olFolderInbox)
  Set objFolder = objInbox.Folders("Done")
  Set objSecFolder = objInbox.Folders("Experts Exchange")
  
  'Assume this is a mail folder
  If objFolder Is Nothing Then
    MsgBox "This folder doesn't exist!", vbOKOnly + vbExclamation, "INVALID FOLDER"
  End If
  
 'Check Second Folder
 If objSecFolder Is Nothing Then
    MsgBox "This folder doesn't exist!", vbOKOnly + vbExclamation, "INVALID FOLDER"
  End If
  
  
  
  Dim a, b As Variant
  Dim SenderDomain As String
  
  a = Application.ActiveExplorer.Parent.Panes.Class
  b = Application.ActiveExplorer.Caption
  If Application.ActiveExplorer.Selection.Count = 0 Then
    'Require that this procedure be called only when a message is selected
    Exit Sub
  End If
  For Each objItem In Application.ActiveExplorer.Selection
    If objFolder.DefaultItemType = olMailItem And objItem.Class = olMail Then
      'clear categories
      objItem.Categories = ""
      'mark item as read
      objItem.UnRead = False
        'Clear Flag
        If objItem.FlagStatus = 2 Then
          objItem.TaskCompletedDate = Date
          Else
        End If
      objItem.UnRead = False 'Move to Designated Folder
    
    SenderDomain = objItem.SenderEmailAddress
    SenderDomain = InStr(1, objItem.SenderEmailAddress, "@", vbTextCompare)
    Select Case SenderDomain
      Case "experts-exchange.com"
      objItem.Move objSecFolder
      Case Else
      objItem.Move objFolder
    End Select
    End If
  Next

  Set objItem = Nothing
  Set objFolder = Nothing
  Set objInbox = Nothing
  Set objNS = Nothing
End Sub

Open in new window

0
Chris BottomleySoftware Quality Lead EngineerCommented:
bromy2004:

FWIW I cannort see a delete or move of an email in this script therefore yes it will work it is only when the collection is reduced dynamically that for each is a problem.

Cmitch's script was deleting files therefore the collection was reducing and hence the half split with every run.

Chris
0
bromy2004Commented:
Lines 45-54 are the Move
    Select Case SenderDomain
      Case "experts-exchange.com"
      objItem.Move objSecFolder
      Case Else
      objItem.Move objFolder
    End Select
0
Chris BottomleySoftware Quality Lead EngineerCommented:
ok missed that and in that case it will fail ... perhaps mails other than the selection are processed because I most certainly would expect it to fail when moving every mail item in the folder.

And it's a general issue with collections as far as I am aware not just outlook mail items.

Chris
0
bromy2004Commented:
??
I'm a bit confused now.
I tested with Deleting and Moving (Separate Macros) all items in a Folder...and it worked fine.
0
Chris BottomleySoftware Quality Lead EngineerCommented:
Tis still the case that it works as I mentioned in Vista and office 2007 - what are you using?

Chris
0
bromy2004Commented:
I've got XP and Office 2010 Beta.

Nathan
0
Chris BottomleySoftware Quality Lead EngineerCommented:
I've had a pootle but unfortunately cannot see a reference to model changes ... perhaps it is fixed in Office 2010 then.

Not that i'll be upgrading any time soon!

Chris
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Outlook

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.