Outlook Banter

Outlook Banter (http://www.outlookbanter.com/)
-   Outlook and VBA (http://www.outlookbanter.com/outlook-vba/)
-   -   Add ContactItems to DLItem with VBS (http://www.outlookbanter.com/outlook-vba/14193-add-contactitems-dlitem-vbs.html)

Christoph Fricke May 9th 06 06:56 PM

Add ContactItems to DLItem with VBS
 
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", "
' 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
;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


Sue Mosher [MVP-Outlook] May 9th 06 07:15 PM

Add ContactItems to DLItem with VBS
 
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
http://www.turtleflock.com/olconfig/index.htm
and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
http://www.outlookcode.com/jumpstart.aspx

"Christoph Fricke" wrote in message oups.com...
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", "
' 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
;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



Dmitry Streblechenko May 9th 06 08:08 PM

Add ContactItems to DLItem with VBS
 
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.
plugRedemption allows you to a contact to a DL programmatically using
RDOContactItem.AddContact - see
http://www.dimastr.com/redemption/rd...em.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" wrote in message
oups.com...
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", "
' 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
;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





Christoph Fricke May 9th 06 08:21 PM

Add ContactItems to DLItem with VBS
 
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


Dmitry Streblechenko May 9th 06 09:09 PM

Add ContactItems to DLItem with VBS
 
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

"Christoph Fricke" wrote in message
ups.com...
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




Christoph Fricke May 9th 06 09:55 PM

Add ContactItems to DLItem with VBS
 
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


Dmitry Streblechenko May 9th 06 10:37 PM

Add ContactItems to DLItem with VBS
 
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

"Christoph Fricke" wrote in message
oups.com...
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




Christoph Fricke May 10th 06 02:17 PM

Add ContactItems to DLItem with VBS
 
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


Dmitry Streblechenko May 10th 06 07:03 PM

Add ContactItems to DLItem with VBS
 
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

"Christoph Fricke" wrote in message
oups.com...
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





All times are GMT +1. The time now is 08:50 PM.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 2.4.0
Copyright ©2004-2006 OutlookBanter.com