检测 Outlook 中是否存在(Exchange 的)公用文件夹
使用以下代码块选择 Outlook 中设置的公用文件夹:
const
olPublicContactsFolder = $00000012; //constant for the public folder
begin
Outlook := CreateOleObject('Outlook.Application');
// Get name space
NameSpace := Outlook.GetNameSpace('MAPI');
// Get root public folder
ContactsRoot := NameSpace.GetDefaultFolder(olPublicContactsFolder); //<-- Error
Contacts:= Contactsroot;
如果 Outlook 中没有公用文件夹(Outlook 中没有设置公用文件夹,没有 Exchange Server),则标记行上会出现错误。
问题是如何通过预先检测是否设置了公用文件夹来避免该错误。
使用try...finally/ except 块捕获错误不起作用由于该异常是由 Microsoft API (EOleException) 从外部引起的。
我想不出另一种方法来检测该文件夹是否存在,因为导致错误的行对于选择公用文件夹并因此获取其属性至关重要。
问候
With the following Code-Block you select the public folder which is set in Outlook:
const
olPublicContactsFolder = $00000012; //constant for the public folder
begin
Outlook := CreateOleObject('Outlook.Application');
// Get name space
NameSpace := Outlook.GetNameSpace('MAPI');
// Get root public folder
ContactsRoot := NameSpace.GetDefaultFolder(olPublicContactsFolder); //<-- Error
Contacts:= Contactsroot;
An error occurs on the marked line if there is no public folder in Outlook (no public folder set in Outlook, no Exchange Server).
The question is how to avoid that error by previously detecting if there is a public folder set or not.
Catching the error with a try...finally/except block didn't work as the exception is caused externally by the Microsoft API (EOleException).
I can't think of another way to detect if the folder exists as the line causing the error is essential in selecting the public folder and therefore getting properties of it.
greetings
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为什么不尝试/除了工作? Delphi 可以很好地捕获 EOleSysError 异常。
该异常是由 Delphi RTL 引发的,而不是由 Outlook 引发的 - 所有 IDispatch 友好的库都会返回一个错误代码,RTL 在使用 IErrorInfo 请求描述后将其转换为 OLE 异常。
Why wouldn't try/except work? Delphi catches EOleSysError exceptions just fine.
And the exception is raised by the Delphi RTL, not Outlook - all IDispatch-friendly libraries return an error code, which the RTL converts to an OLE exception after requesting the description using IErrorInfo.
正如消息所示,Outlook 不知道要使用哪个配置文件。您需要先登录到 MAPI 命名空间,然后才能对其执行任何操作。即使没有登录信息,例如当您连接到未连接到 Exchange 服务器的本地 Outlook 实例时,您也需要执行此操作。
As the message indicates, Outlook doesn't know which profile to use. You need to logon to the MAPI namespace before you can do anything with it. You need to do this even if there is no logon information for example when you connect to a local Outlook instance that is not connected to an Exchange server.
这个代码块确实工作得很好。我想我还有另一个问题已经解决了。感谢您抽出时间。
Dmitry Streblechenko 的回答非常正确,我只是将调试器的 EOleException 误认为是外部异常。在没有调试器的情况下运行代码不会导致异常,因为它确实被 try/exception 块捕获。
在 Outlook 中使用多个帐户时,Marjan Venemas 的答案可能会很方便。
This Codeblock does work just fine. I figured I had another issue which I resolved. Thanks for your time.
Dmitry Streblechenko was quite right with his answer, I just mistook the EOleException of the Debugger for an external one. Running the Code without the Debugger doesn't lead to the Exception as it indeed gets catched by the try/exception block.
Marjan Venemas answer might come handy when working with multiple accounts in Outlook.