Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 653
  • Last Modified:

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

0
Cmitch
Asked:
Cmitch
  • 6
  • 6
  • 2
1 Solution
 
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 BottomleyCommented:
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
Granular recovery for Microsoft Exchange

With Veeam Explorer for Microsoft Exchange you can choose the Exchange Servers and restore points you’re interested in, and Veeam Explorer will present the contents of those mailbox stores for browsing, searching and exporting.

 
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 BottomleyCommented:
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 BottomleyCommented:
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 BottomleyCommented:
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 BottomleyCommented:
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 BottomleyCommented:
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

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 6
  • 6
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now