Add ContactItems to DLItem with VBS

  • Thread starter Christoph Fricke
  • Start date
C

Christoph Fricke

Hi there,

I want to create a Outlook Distributionlist based on a csv file with
VBS. The script works fine with one limitation.

If the e-mail address is found within the GAL Object and I double click
the ContactItem in the DistributionListItem after the script ran
through the GAL user object is shown. If I add a contact with the same
script whose e-mail address cannot be found within the GAL object and I
double click it, only the e-mail properties of this item are shown
(display name, e-mail address etc.).

The intention of the script is to create only the last type of
ContactItems. Is this possible? If yes, where is the error? I'll add
the raw test code below.

And some other question: Is it possible to check if a
DistributionListItem already exists with VBS?

Thank you in advance for pointing me in the right direction. :)

Christoph

P.S. The DL already exists during scriptpart execution

Code:
' add User to DL
addNewDistListMember "DL Test", "Annoying", "[email protected]"
' add DL to DL
addNewDistListMember "DL Test", "DL Test2", "Unknown"

Sub addNewDistListMember(strDistListName, strDistListMemberName,
strDistListMemberMail)

Dim objOutlookApp 'As New Outlook.Application
Dim objOutlookNamespace '
Dim objOutlookContacts '
Dim objDistListItem 'As DistListItem
Dim objMailItem 'As MailItem
Dim objRcpnt 'As Recipient

Set objOutlookApp = CreateObject("Outlook.Application")
Set objOutlookNamespace = objOutlookApp.GetNamespace("MAPI")
Set objOutlookContacts = objOutlookNamespace.GetDefaultFolder(10) '
olFolderContacts

For i = 1 To objOutlookContacts.Items.Count

If TypeName(objOutlookContacts.Items.Item(i)) = "DistListItem" Then

Set objDistListItem = objOutlookContacts.Items.Item(i)

If objDistListItem.DLName = strDistListName Then

Set objMailItem = objOutlookApp.CreateItem(olMailItem)
' if CSV Entry is build like this: DL Test2;DL
Test;Unknown;DistListItem
If instr(strDistListMemberMail, "Unknown") Then
' add DL to DL
Set objRcpnt = objMailItem.Recipients.Add(strDistListMemberName)
If objRcpnt.Resolve Then
wscript.echo "	Adding '" & strDistListMemberName & "' to DL '" &
_
strDistListName & "'"
objDistListItem.AddMember objRcpnt
End If
objDistListItem.Save

' if CSV Entry is build like this: DL
Test;Name;[email protected];ContactItem
Else
' add user to DL
Set objRcpnt = objMailItem.Recipients.Add(strDistListMemberName &
Chr(32) & strDistListMemberMail)

If objRcpnt.Resolve Then

wscript.echo "	Adding '" & strDistListMemberName & "' to DL '" &
_
strDistListName & "'"

objDistListItem.AddMember objRcpnt
End If
objDistListItem.Save

End If
End If

End If

Next

Set objOutlookApp = Nothing
Set objOutlookNamespace = Nothing
Set objOutlookContacts = Nothing
Set objDistListItem = Nothing
Set objMailItem = Nothing
Set objRcpnt = Nothing

End Sub
 
S

Sue Mosher [MVP-Outlook]

There's no error. Everything you've described is normal, expected behavior. A GAL user entry in a DL always points to the GAL record.

If you want to know whether an entry already exists, you'll have to iterate the DL's Members property and check each member's address or name.

--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers


Christoph Fricke said:
Hi there,

I want to create a Outlook Distributionlist based on a csv file with
VBS. The script works fine with one limitation.

If the e-mail address is found within the GAL Object and I double click
the ContactItem in the DistributionListItem after the script ran
through the GAL user object is shown. If I add a contact with the same
script whose e-mail address cannot be found within the GAL object and I
double click it, only the e-mail properties of this item are shown
(display name, e-mail address etc.).

The intention of the script is to create only the last type of
ContactItems. Is this possible? If yes, where is the error? I'll add
the raw test code below.

And some other question: Is it possible to check if a
DistributionListItem already exists with VBS?

Thank you in advance for pointing me in the right direction. :)

Christoph

P.S. The DL already exists during scriptpart execution

Code:
' add User to DL
addNewDistListMember "DL Test", "Annoying", "[email protected]"
' add DL to DL
addNewDistListMember "DL Test", "DL Test2", "Unknown"

Sub addNewDistListMember(strDistListName, strDistListMemberName,
strDistListMemberMail)

Dim objOutlookApp 'As New Outlook.Application
Dim objOutlookNamespace '
Dim objOutlookContacts '
Dim objDistListItem 'As DistListItem
Dim objMailItem 'As MailItem
Dim objRcpnt 'As Recipient

Set objOutlookApp = CreateObject("Outlook.Application")
Set objOutlookNamespace = objOutlookApp.GetNamespace("MAPI")
Set objOutlookContacts = objOutlookNamespace.GetDefaultFolder(10) '
olFolderContacts

For i = 1 To objOutlookContacts.Items.Count

If TypeName(objOutlookContacts.Items.Item(i)) = "DistListItem" Then

Set objDistListItem = objOutlookContacts.Items.Item(i)

If objDistListItem.DLName = strDistListName Then

Set objMailItem = objOutlookApp.CreateItem(olMailItem)
' if CSV Entry is build like this: DL Test2;DL
Test;Unknown;DistListItem
If instr(strDistListMemberMail, "Unknown") Then
' add DL to DL
Set objRcpnt = objMailItem.Recipients.Add(strDistListMemberName)
If objRcpnt.Resolve Then
wscript.echo " Adding '" & strDistListMemberName & "' to DL '" &
_
strDistListName & "'"
objDistListItem.AddMember objRcpnt
End If
objDistListItem.Save

' if CSV Entry is build like this: DL
Test;Name;[email protected];ContactItem
Else
' add user to DL
Set objRcpnt = objMailItem.Recipients.Add(strDistListMemberName &
Chr(32) & strDistListMemberMail)

If objRcpnt.Resolve Then

wscript.echo " Adding '" & strDistListMemberName & "' to DL '" &
_
strDistListName & "'"

objDistListItem.AddMember objRcpnt
End If
objDistListItem.Save

End If
End If

End If

Next

Set objOutlookApp = Nothing
Set objOutlookNamespace = Nothing
Set objOutlookContacts = Nothing
Set objDistListItem = Nothing
Set objMailItem = Nothing
Set objRcpnt = Nothing

End Sub
 
D

Dmitry Streblechenko

In this case you will need to create a contact first (ContactItem object),
then manually add it to the DL (when you click "Select Members" as opposed
to "Add New"). Outlook links to an existing contact based on its entry id,
not on its e-mail address.
<plug>Redemption allows you to a contact to a DL programmatically using
RDOContactItem.AddContact - see
http://www.dimastr.com/redemption/rdo/RDODistListItem.htm#methods </plug>

To check whether a DL with a given name already exists, simply us
MAPIFolder.Items("DistList Name"). If a DL list named "DistList Name" does
not exist, you will get an error (which you can trap).

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool

Christoph Fricke said:
Hi there,

I want to create a Outlook Distributionlist based on a csv file with
VBS. The script works fine with one limitation.

If the e-mail address is found within the GAL Object and I double click
the ContactItem in the DistributionListItem after the script ran
through the GAL user object is shown. If I add a contact with the same
script whose e-mail address cannot be found within the GAL object and I
double click it, only the e-mail properties of this item are shown
(display name, e-mail address etc.).

The intention of the script is to create only the last type of
ContactItems. Is this possible? If yes, where is the error? I'll add
the raw test code below.

And some other question: Is it possible to check if a
DistributionListItem already exists with VBS?

Thank you in advance for pointing me in the right direction. :)

Christoph

P.S. The DL already exists during scriptpart execution

Code:
' add User to DL
addNewDistListMember "DL Test", "Annoying", "[email protected]"
' add DL to DL
addNewDistListMember "DL Test", "DL Test2", "Unknown"

Sub addNewDistListMember(strDistListName, strDistListMemberName,
strDistListMemberMail)

Dim objOutlookApp 'As New Outlook.Application
Dim objOutlookNamespace '
Dim objOutlookContacts '
Dim objDistListItem 'As DistListItem
Dim objMailItem 'As MailItem
Dim objRcpnt 'As Recipient

Set objOutlookApp = CreateObject("Outlook.Application")
Set objOutlookNamespace = objOutlookApp.GetNamespace("MAPI")
Set objOutlookContacts = objOutlookNamespace.GetDefaultFolder(10) '
olFolderContacts

For i = 1 To objOutlookContacts.Items.Count

If TypeName(objOutlookContacts.Items.Item(i)) = "DistListItem" Then

Set objDistListItem = objOutlookContacts.Items.Item(i)

If objDistListItem.DLName = strDistListName Then

Set objMailItem = objOutlookApp.CreateItem(olMailItem)
' if CSV Entry is build like this: DL Test2;DL
Test;Unknown;DistListItem
If instr(strDistListMemberMail, "Unknown") Then
' add DL to DL
Set objRcpnt = objMailItem.Recipients.Add(strDistListMemberName)
If objRcpnt.Resolve Then
wscript.echo " Adding '" & strDistListMemberName & "' to DL '" &
_
strDistListName & "'"
objDistListItem.AddMember objRcpnt
End If
objDistListItem.Save

' if CSV Entry is build like this: DL
Test;Name;[email protected];ContactItem
Else
' add user to DL
Set objRcpnt = objMailItem.Recipients.Add(strDistListMemberName &
Chr(32) & strDistListMemberMail)

If objRcpnt.Resolve Then

wscript.echo " Adding '" & strDistListMemberName & "' to DL '" &
_
strDistListName & "'"

objDistListItem.AddMember objRcpnt
End If
objDistListItem.Save

End If
End If

End If

Next

Set objOutlookApp = Nothing
Set objOutlookNamespace = Nothing
Set objOutlookContacts = Nothing
Set objDistListItem = Nothing
Set objMailItem = Nothing
Set objRcpnt = Nothing

End Sub
 
C

Christoph Fricke

Sue,

thank you for answering my question.

I understand that this is the expected behavior. So I will try to
explain my "script intention" more precisely: :)

I choose one Distributionlist that already exists, click "add new" (not
"choose member" [Actually I only have a german outlook client installed
so I do not know the correct english phrase for the buttons (I use
OL2k3)]) and enter the name for the object "<surname>, <givenname>" and
the correct e-mail address, hit OK and the object is added to the
Distributionlist. Whether he is in the GAL or not if I double click
this member only the e-mail properties of this member item are shown,
not the corresponding GAL object.

What I try to do is not to add the / a GAL object. I only want to add a
new member (with the desired name and e-mail address). Maybe I must use
an other way to accomplish the steps described and coded in my first
posting!?

Can you point me in the right direction?

Christoph
 
D

Dmitry Streblechenko

This is a limitation of the Outlook Object Model - you can create a one-off
entry id explicitly in MAPI (in this case Outlook will only show the
name/address dialog). In the Outlook Object Model however, even if you pass
an SMTP address, the GAL address book provider resolves it ot a GAL entry if
the SMTP address matches one of the proxy SMTP addresses. There is no way to
overwrite this behavior to the best of my knowledge.

Sorry for another plug, but in Redemption you can work around that by usingg
RDODistListItem.AddMemberEx method - it will create a one-off entry id for
you using the specified name/address/address type without going through
IAddrBook::ResolveName().

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool
 
C

Christoph Fricke

Dmitry,

will you be so kind and give me clue how to accomplish the check for
the DL existence? I just want to create the DL if it does not exist and
go further with the membership add if it does exist...

Christoph
 
D

Dmitry Streblechenko

Something along the lines (off the top of my head):

on Error resume next
Err.Clear
set DL = MAPIFolder.Items("DL Name")
if Err.Number <> 0 Then
'the DL does not exist
Err.Clear
set DL = MAPIFolder.Items.Add("IPM.DistList")
DL.DLName = "DL Name"
End If
'do something here
DL.Save

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool
 
C

Christoph Fricke

Thank you for your helpful support.

Is it possible to check if an item is already member in this DL by look
for the email used or the name of the item in the DistributionList?
Which property is best to look after?

Christoph
 
D

Dmitry Streblechenko

Loop through all the members of the DL (DistListItem.GetMember) and check
the e-mail address (Recpient.Address).

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top