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