Having trouble with Code to Move Mail

  • Thread starter Thread starter Cumulous128
  • Start date Start date
C

Cumulous128

Hi! I'm having trouble with a bit of VBA in Outlook 2002. The purpose
of this macro is to move the entire contents of one specific folder to
another specific folder.

The problem is this: When I use the version of this macro that has both
Source and Destination folders located **WITHIN** the Inbox - it works fine.

However, when I use the version that works with Source and Destination
folders located OUTSIDE the Inbox - at the Root level of the tree - I get an
error message:

"The operation failed. An object could not be found" - This is referring
to:

-------------
Set objmyFolder = objNameSpace.Folders("Test - Source")
-------------


Can anyone help me figure out where the problem is? The entire Macro
(which includes both versions) is listed below: (And if there are any other
areas that could be improved, I would appreciate the help :)



-------------------------------------------------------------


Sub MoveEmail()

Dim objOutlook As Outlook.Application
Dim objNameSpace As Outlook.NameSpace
Dim objCurrentInsp As Outlook.Inspector
Dim objCurrentItem As Outlook.MailItem
Dim objInboxFolders As Outlook.Folders
Dim objDestination As Outlook.MAPIFolder

Dim OlMail As Object


Set objOutlook = CreateObject("Outlook.Application")
Set objNameSpace = objOutlook.GetNamespace("MAPI")


' ***********

' Enable either Section 1 or Section 2 - but not both at the same time


' 1 - Enable this Section when both Folders are in the Root (This is the
section that does NOT work)
Set objmyFolder = objNameSpace.Folders("Test - Source")
Set objDestination = objNameSpace.Folders("Test - Destination")


' 2 - Enable this Section when both Folders are located UNDER the Inbox
(This section seems to work fine)
'Set objInboxFolders = objNameSpace.GetDefaultFolder(olFolderInbox).Folders
'Set objMyFolder = objInboxFolders.Item("Test - Source")
'Set objDestination = objInboxFolders.Item("Test - Destination")

' ***********




' Get the number of items in the folder.
NumItems = objmyFolder.Items.Count
' Set MyItem to the collection of items in the folder.
Set MyItems = objmyFolder.Items

' Loop through all of the items in the folder.
'For I = 1 To NumItems
' MyItems.Move objDestination
'Next


Do Until objmyFolder.Items.Count = 0
Set MyItems = objmyFolder.Items
For Each OlMail In MyItems
OlMail.Move objDestination
Next
Loop



Set objDestination = Nothing
Set objmyFolder = Nothing


Set objNameSpace = Nothing
Set objOutlook = Nothing

End Sub

-----------------------------------------------------------------------


Thanks!



Cumulous
 
NameSpace.Folders might refer to any one of a number of folders,
including multiple PST files. If your user created folders are at the
same level as the Inbox in that same folder tree try this code:

Set oFolderParent =
objNameSpace.GetDefaultFolder(olFolderInbox).Parent
Set objMyFolder = oFolderParent.Folders("Test - Source")
Set objDestination = oFolderParent.Folders("Test - Destination")
 
Thank you, Ken - that did the trick nicely. :)

As a quick follow-up, however, should I add the following line to the
end of the procedure?

-------------------------------

Set oFolderParent = Nothing

-------------------------------



Cumulous
 
Back
Top