Moving Items

M

Mark Allen

Hi,

I am having trouble with the following code.
I want to move all items from the one folder to another.
I can only seem to move half of the items in the folder.
Is there any mistake in the code?

Thanks in advance

Mark

ption Explicit
Private WithEvents objOLApp As Outlook.Application
Private Sub AddinInstance_OnConnection(ByVal Application
As Object, _
ByVal ConnectMode As
AddInDesignerObjects.ext_ConnectMode, _
ByVal AddInInst As Object, custom() As Variant)
Set objOLApp = Application
End Sub
Private Sub objOLApp_ItemSend(ByVal Item As Object, Cancel
As Boolean)
Dim objNamespace As Outlook.NameSpace
Dim objSentbox As Outlook.MAPIFolder
Dim objSentFolder As Outlook.MAPIFolder
Dim objDefSentFolder As Outlook.MAPIFolder
Set objNamespace = objOLApp.GetNamespace("MAPI")
Set objDefSentFolder = objNamespace.GetDefaultFolder
(olFolderSentMail)
Set objSentbox = objNamespace.Folders("Local Folders
stored on h drive")
Set objSentFolder = objSentbox.Folders("Sent Items")
Item.SaveSentMessageFolder objSentFolder
If objDefSentFolder.Items.Count > 0 Then
For Each Item In objDefSentFolder.Items
Item.Move objSentFolder
Next Item
End If
Set objSentbox = Nothing
Set objSentFolder = Nothing
Set objDefSentFolder = Nothing
Set objNamespace = Nothing
End Sub
Private Sub AddinInstance_OnDisconnection(ByVal RemoveMode
As _
AddInDesignerObjects.ext_DisconnectMode, custom
() As Variant)
Set objOLApp = Nothing
End Sub
 
M

Mark H. Shin

When you move an item in a collection, it affects the
collection itself...

To get around this well known problem, try:

N = objDefSentFolder.Items.Count
For I = N To 1 Step -1
objDefSentFolder.Items(I).Move objSentFolder
Next
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top