使用 VBA 访问 Outlook 中的文件夹

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12624135/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 17:53:03  来源:igfitidea点击:

Accessing folders in Outlook with VBA

vbaoutlooklivelink

提问by dan

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

我正在使用以下内容将邮件移动到 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.

一切实际上都在起作用。如您所见,该文件夹位于 Livelink 中。而且 Livelink 服务器实际上响应速度很慢,我对此无能为力。

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

我担心的是多次使用 .Folders() 而这样做会快很多.Folders("Livelink/root/folder1/folder2/")。但这显然不起作用,因为 .Folders 命令每次都需要 ping Livelink 服务器,所以执行这行代码实际上需要整整 10 秒(而且文件夹越深,到达的时间就越长)它)。

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:

有没有其他方法可以直接访问 Outlook 中的特定文件夹以移动邮件?我知道每个文件夹(甚至是 Livelink 中的文件夹)都有某种 Outlook ID,但我看不到任何使用它的方法。我已经尝试了以下方法,但还没有奏效:

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 targetis actually the EntryID of the folder I want to copy the mail to.

这在执行 GetFolderfromID() 时给我一个错误。vartarget实际上是我要将邮件复制到的文件夹的 EntryID。

回答by Daniel

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 pathbut this basically does the same thing you are already doing.

MSDN 建议的一个选项是从文件夹路径中获取文件夹对象,但这基本上与您已经在做的事情相同。

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

问题是文件夹对象仅表示“在文件夹树的一个级别上代表特定子集中的所有可用 Outlook 文件夹。”(强调添加)

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 EntryIDand StoreIDfor future immediate recall.

一种可能的解决方法是使用NameSpace.GetFolderFromID,但为此您需要知道EntryID,也可能是StoreID,这通常意味着您必须首先找到文件夹。但是您可以保存EntryIDStoreID以备将来立即召回。

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

如果您想深入了解EntryIds 和StoreIDs 的使用,这里是有关使用 EntryIDs 和 StoreIDs的开发人员参考。