Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Having trouble with Code to Move Mail

5 views
Skip to first unread message

Cumulous128

unread,
Dec 21, 2003, 11:22:48 PM12/21/03
to

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


Ken Slovak - [MVP - Outlook]

unread,
Dec 22, 2003, 9:08:38 AM12/22/03
to
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")

--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Absolute Beginners Guide to Microsoft Office Outlook 2003
Reminder Manager, Extended Reminders, Attachment Options
http://www.slovaktech.com/products.htm


"Cumulous128" <Cumul...@yahoo.com> wrote in message
news:er#sCNEyD...@TK2MSFTNGP10.phx.gbl...

Cumulous128

unread,
Dec 22, 2003, 12:01:36 PM12/22/03
to

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


"Ken Slovak - [MVP - Outlook]" <kens...@mvps.org> wrote in message
news:%23WAhYWJ...@TK2MSFTNGP10.phx.gbl...

Ken Slovak - [MVP - Outlook]

unread,
Dec 22, 2003, 2:38:30 PM12/22/03
to
Always release all your Outlook objects explicitly.

--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Absolute Beginners Guide to Microsoft Office Outlook 2003
Reminder Manager, Extended Reminders, Attachment Options
http://www.slovaktech.com/products.htm


"Cumulous128" <Cumul...@yahoo.com> wrote in message

news:#KJwD1Ky...@TK2MSFTNGP09.phx.gbl...

0 new messages