Link to home
Start Free TrialLog in
Avatar of jlalande
jlalande

asked on

How do I move an item from one folder to another in VBScript?

In a previous question, I asked and learned how to get a reference to the currently selected email.  I've done some work with it and now I'd like to move it to another folder.
Can someone explain this process to me?
Thanks
Avatar of Flash828
Flash828

This code is from an MSDN page:

URL: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbaol10/html/olmthMove.asp

Set myOlApp = CreateObject("Outlook.Application")
Set myNameSpace = myOlApp.GetNameSpace("MAPI")
Set myInbox = myNameSpace.GetDefaultFolder(olFolderInbox)
Set myItems = myInbox.Items
Set myDestFolder = myInbox.Folders("Personal Mail")
Set myItem = myItems.Find("[SenderName] = 'Mike Nash'")
While TypeName(myItem) <> "Nothing"
    myItem.Move myDestFolder
    Set myItem = myItems.FindNext
Wend
If you use VBScript, you do not create the Application object, and you cannot use named constants. This example shows how to perform the same task using VBScript.

Set myNameSpace = Application.GetNameSpace("MAPI")
Set myInbox = myNameSpace.GetDefaultFolder(6)
Set myItems = myInbox.Items
Set myDestFolder = myInbox.Folders("Personal Mail")
Set myItem = myItems.Find("[SenderName] = 'Mike Nash'")
While TypeName(myItem) <> "Nothing"
    myItem.Move myDestFolder
    Set myItem = myItems.FindNext
Wend
ASKER CERTIFIED SOLUTION
Avatar of Flash828
Flash828

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jlalande

ASKER

This works fine if "Personal Mail" is a subfolder of "Inbox".  The folder I would like to move my message is to a folder at the same level as "Inbox".
OK.
I figured out how to get my folder.  Instead of

Set myDestFolder = myInbox.Folders("Scratch")

I did

Set myDestFolder = myInbox.Parent.Folders("Scratch")

Thanks for your help.