检测 Outlook 中是否存在(Exchange 的)公用文件夹

发布于 2024-12-10 10:49:27 字数 699 浏览 0 评论 0原文

使用以下代码块选择 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

零度° 2024-12-17 10:49:27

为什么不尝试/除了工作? 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.

浪荡不羁 2024-12-17 10:49:27

正如消息所示,Outlook 不知道要使用哪个配置文件。您需要先登录到 MAPI 命名空间,然后才能对其执行任何操作。即使没有登录信息,例如当您连接到未连接到 Exchange 服务器的本地 Outlook 实例时,您也需要执行此操作。

FNameSpace := FOutlook.GetNamespace('MAPI');
FNameSpace.Logon('', '', False, False);
Folder := FNameSpace.GetDefaultFolder( olFolderCalendar );
Memo1.Lines.Add( 'Calendar: ' + Folder.Name + ': ' + Folder.Description );

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.

FNameSpace := FOutlook.GetNamespace('MAPI');
FNameSpace.Logon('', '', False, False);
Folder := FNameSpace.GetDefaultFolder( olFolderCalendar );
Memo1.Lines.Add( 'Calendar: ' + Folder.Name + ': ' + Folder.Description );
一杯敬自由 2024-12-17 10:49:27

这个代码块确实工作得很好。我想我还有另一个问题已经解决了。感谢您抽出时间。

function DoesPublicFolderExist():Boolean;
const
  olFolderContacts = $00000012;
var
  Outlook, Namespace, ContactsRoot, Contactsfolder : OleVariant;
begin
  // Connect to outlook
  Outlook := CreateOleObject('Outlook.Application');
  // Get name space
  NameSpace := Outlook.GetNameSpace('MAPI');
  // Get root contacts folder
  try
    ContactsRoot := NameSpace.GetDefaultFolder(olFolderContacts);
    Result:= True;
  except
    Result:= False;
  end;
end;

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.

function DoesPublicFolderExist():Boolean;
const
  olFolderContacts = $00000012;
var
  Outlook, Namespace, ContactsRoot, Contactsfolder : OleVariant;
begin
  // Connect to outlook
  Outlook := CreateOleObject('Outlook.Application');
  // Get name space
  NameSpace := Outlook.GetNameSpace('MAPI');
  // Get root contacts folder
  try
    ContactsRoot := NameSpace.GetDefaultFolder(olFolderContacts);
    Result:= True;
  except
    Result:= False;
  end;
end;

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文