Accessing folders in Outlook with VBA

2019-05-25 08:05发布

I'm using the following to move a mail to a folder in Outlook.

Dim chemin() as String

chemin = Split(path, "/")
Set myNameSpace = Application.GetNamespace("MAPI")
Set myFolder = myNameSpace.Folders("LiveLink").Folders("Livelink HQE").Folders("Entreprise").Folders(chemin(1)).Folders(chemin(2)).Folders(chemin(3))

myEntryID = myFolder.EntryID
myEntryID = myFolder.StoreID

objMail.Move myNameSpace.GetFolderFromID(myEntryID, storeID)    

Everything is actually working. As you can see, the folder is located into Livelink. And the Livelink server is actually quite slow to respond, and I can't do anything about it.

My concern is about using the .Folders() so many times while it would be A LOT faster doing something like .Folders("Livelink/root/folder1/folder2/"). But this isn't working obviously, and because the .Folders command needs to ping the Livelink server everytime, it actually takes a whole 10 seconds just to execute this line of code (and the deeper is the folder, the longer it is to reach it).

Is there any other way to directly access a specific folder in Outlook to move a mail? I know there is some kind of Outlook ID for each folders (even those in Livelink) but I don't see any way to make use of it. I've tried the following, but it's not working yet:

Dim folder As MAPIFolder
Dim myNameSpace As Outlook.NameSpace
Set myNameSpace = Application.GetNamespace("MAPI")
Set folder = myNameSpace.GetFolderFromID(target, Application.GetNamespace("MAPI").Folders("LiveLink").storeID)

This gives me an error when doing GetFolderfromID(). The var target is actually the EntryID of the folder I want to copy the mail to.

1条回答
老娘就宠你
2楼-- · 2019-05-25 08:45

Based on the official documentation, there is no better way than what you are doing, unless you will need to find the folder multiple times.

An option suggested by MSDN is to obtain the folder object from the folder path but this basically does the same thing you are already doing.

The problem is that the Folder Object only represents "represent all the available Outlook folders in a specific subset at one level of the folder tree."(emphasis added)

A possible work-around would be to use NameSpace.GetFolderFromID, but for that you would need to know the EntryID, and possibly the StoreID, which usually means you have to find the folder first anyway. But you could save the EntryID and StoreID for future immediate recall.

If you want to delve into using EntryIds and StoreIDs here is a developers reference on Working with EntryIDs and StoreIDs.

查看更多
登录 后发表回答