外部应用程序:检查 Outlook 文件夹是否存在

发布于 2024-09-19 15:01:27 字数 1780 浏览 4 评论 0原文

下面的解决方案

我一直在网上寻找解决方案,但在 Delphi 中似乎很难得到这个问题的答案...

如果您熟悉 Outlook,请跳过此部分

之前的一些解释: Outlook 中的联系人文件夹的组织方式类似于 Windows 中的文件夹树。联系人存储在联系人文件夹本身或子文件夹中。

我的代码确实将外部数据库中的联系人添加到 Outlook 联系人数据库中。为了防止重复输入,程序应该检查所有联系人,看看是否可以找到联系人条目的“旧”版本并更新它,如果不能,则创建一个新版本。
因此,我编写了一个递归程序,循环遍历文件夹并检查联系人。

在文件夹中,您可以通过以下方式获取子文件夹(除了下一个、上一个和最后一个)

Contacts:= Contacts.Folders.Getfirst

//现在选择的文件夹是上一个选择的文件夹中的第一个子文件夹

如果我试图获取此子文件夹的任何属性,例如“Items.Count”或否则,会发生错误,因为该文件夹不存在。
因此,我想检查该文件夹是否存在,并跳到循环遍历该子文件夹,因为否则循环将在此处中断并且程序停止。

如果您熟悉 Outlook 的工作原理,请跳至此处
问题:

在调试器中,此联系人/文件夹变量(OleVariant,指向当前选定文件夹的指针)包含类似于以下内容的值:“$0074974C”。
如果没有子文件夹,则该值返回“$00000000”。这似乎是一个指针。

我应该如何检查文件夹是否存在?

const
  olFolderContacts = $0000000A;
var
  outlook, NameSpace, Contact, ContactsRoot, Contacts: OleVariant;

begin
Outlook := CreateOleObject('Outlook.Application');
NameSpace := Outlook.GetNameSpace('MAPI');
ContactsRoot := NameSpace.GetDefaultFolder(olFolderContacts);
Contacts:= ContactsRoot;
//We're now in the Contacts Folder
Contacts:= Contacts.folders.getfirst;
//First Subfolder

什么不起作用: 检查

Contacts 是否 = '$00000000'(作为字符串)

Contacts = '$00000000'(作为 OleVariant)

var
val:TVarRec;
代码:
val:=联系人;
string(Contacts.VWideChar) = '$00000000'

var
vntNothing:OLEVariant;
代码:
TVarData(vntNothing).VType := varDispatch;
TVarData(vntNothing).VDispatch := Nil;
联系人 = vntNothing

联系人 = 未分配
...
...

在 VBA 中这个问题有一个简单的解决方案

if Contacts = Nothing

但是 Delphi 中没有“Nothing”...

有想法吗?

SOLUTION BELOW

I've been looking all over the net to find a solution for this, but it seems quite hard to get an answer for this in Delphi...

Skip this if you're familiar with Outlook

Some explanation before:
The Contacts Folder in Outlook is organized like a foldertree in Windows. The Contacts are stored in the Contacts Folder itself or within subfolders.

My Code does add Contacts from an external Database into the Outlook contacts Database. To prevent double entries the programm is supposed to check all contacts and see if it can find an 'older' version of the contact entry and update it, or if not, create a new one.
Therefore I wrote a recursion which loops through the folders and checks the contacts.

Within a folder you can get the subfolder by (besides Next, Previous and Last)

Contacts:= Contacts.Folders.Getfirst

//The now selected Folder is the first subfolder within the previous selected one

If I am trying to get any property of this Subfolder like 'Items.Count' or anything else, an error occurs because this folder doesn't exist.
Therefore I want to check if the Folder exists or not, and skip to loop through this subfolder because otherwise the loop would break here and the program stops.

Skip until here if you're familiar with Outlook workings
THE PROBLEM:

In Debugger this Contacts/Folder Variable (an OleVariant, Pointer to the now selected Folder) contains values similar to this: '$0074974C'.
If there is no subfolder this value returns '$00000000'. This seems to be a pointer.

How should I check if a folder exists or not?

const
  olFolderContacts = $0000000A;
var
  outlook, NameSpace, Contact, ContactsRoot, Contacts: OleVariant;

begin
Outlook := CreateOleObject('Outlook.Application');
NameSpace := Outlook.GetNameSpace('MAPI');
ContactsRoot := NameSpace.GetDefaultFolder(olFolderContacts);
Contacts:= ContactsRoot;
//We're now in the Contacts Folder
Contacts:= Contacts.folders.getfirst;
//First Subfolder

What didn't work:
Check if

Contacts = '$00000000' (As string)

Contacts = '$00000000' (As OleVariant)

var
val:TVarRec;
code:
val:=Contacts;
string(Contacts.VWideChar) = '$00000000'

var
vntNothing: OLEVariant;
code:
TVarData(vntNothing).VType := varDispatch;
TVarData(vntNothing).VDispatch := Nil;
Contacts = vntNothing

Contacts = unassigned
...
...

In VBA this problem has a simple solution

if Contacts = Nothing

But there is no 'Nothing' in Delphi...

Ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

酷到爆炸 2024-09-26 15:02:20
var  
x: string;  

在代码中:

x:= format('%p%',[Pointer(TVarData(contacts).VDispatch)]);
if x = '00000000' then  
   'New Contact'  
else  
   'open folder and search within this one'

同事有解决方案..感谢您的时间:)

var  
x: string;  

in code:

x:= format('%p%',[Pointer(TVarData(contacts).VDispatch)]);
if x = '00000000' then  
   'New Contact'  
else  
   'open folder and search within this one'

Co-worker had the solution.. Thanks for your time :)

︶ ̄淡然 2024-09-26 15:02:11

你可以试试这个:

if IUnknown(Contacts) = nil then
  //

You could try this:

if IUnknown(Contacts) = nil then
  //
胡渣熟男 2024-09-26 15:02:02

您可以首先检查文件夹集合的计数:

if Contacts.Folders.Count = 0 then

Contacts := Contacts.Folders.GetFirst;
if VarIsClear(Contacts) then

You could first check the count on the Folders collection:

if Contacts.Folders.Count = 0 then

or

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