Link to home
Start Free TrialLog in
Avatar of Shen
ShenFlag for United States of America

asked on

vb outlook script

I am  running a vb outlook script to move emails to a personal folder. Currently, the vb script calls a recursive complicated function to do this.  I want to hard code the actual personal path to replace the function call ( getfolderbyname("test").  What is the syntax to move an email to a particular folder.

current code:
Item.Move GetFolderByName("test")  
          Item.Save
want to replace with item.move "folder path/test"  ?
Avatar of Rgonzo1971
Rgonzo1971

Hi,

pls try something like this
Set ns = Application.GetNamespace("MAPI")
Set moveToFolder = ns.Folders("Mailbox - John Doe").Folders("Important")
Item.Move moveToFolder 

Open in new window

Regards
Unfortunately there is no short cut to what you want.  The Outlook object model does not have a function where you can tell it the "full path" to a folder and it can find it.  So you either have to use what you have, or assemble the pieces yourself like Rgonzo1971 showed.

I personally like the method you are using already, I find the ability to reference a folder as "Mailbox - John Doe\Inbox\Memos\Project1" fairly easy.  Yes, you need to helper function, but it's actually not that complicated.  And while I can imagine a recursive approach, most of the ones I have seen (and the one I am using) is an iterative approach.  It just breaks the folder "path" down into the sub folders, and then walks the list of subfolders find each child until it reaches the final folder that you want.  Typically these GetFolder() functions look something like this example, which while it is more than a couple of lines of code, it isn't too complex to follow.

To get a MAPIFolder object in Microsoft Outlook (VBA or VBScript)

I get that you are trying to reduce the 'extra' stuff in your code project, but in this case I think you may be better off in the main parts of your code using that small function.  I find this easier to read:

Item.Move GetFolderByName("Mailbox - John Doe\Inbox\Memos\Project1")

than this:

Item.Move ns.Folders("Mailbox - John Doe").Folders("Inbox").Folders("Memos").Folders("Project1")

There is no way in Outlook to get to the bottom folder without going through each of the parents involved, that's just the way it is...


»bp
NFP:

I totally agree with Bill.  If you do want to do it the second way, then I would lay it out like this:

Item.Move ns.Folders _
    ("Mailbox - John Doe").Folders _
    ("Inbox").Folders
    ("Memos").Folders
    ("Project1")

as I think it is easier to read, but not as easy as Bill's first example.

Alan.
Avatar of Shen

ASKER

So if the mailbox is shen@test.com, and i want to move the emails to folder "test" in personal file: "PersonalFile1" the code syntax should be:

 Dim moveToFolder  As String

Set ns = Application.GetNamespace("MAPI")
Set moveToFolder = ns.Folders("Mailbox - Shen").Folders ("PersonalFile1").Folders("test")
Item.Move moveToFolder
item.save
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

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 Shen

ASKER

this worked. excellent.

thanks,
Welcome, glad that was helpful.


»bp