Creating a DistList from a .txt file

N

NateG

I'm having problems when trying to generate a dist. list in VB.NET.

I'm trying to get a distribution list from a Eudora NNDBase.txt file into
Outlook and not having luck. I have the following data from parsing the
strings in the .txt file:

Dist List Name
Array containing the names of the dist. list members
Array containing the e-mails of the dist. list members

Now, I haven't had much luck with generating the code to create the
distribution list, add the names of the members followed by their e-mails.
Any suggestion?

I'm open to changing the method by which I'm adding members, but keep in
mind the only way to get the information about the members is by parsing a
gigantic 1 line string of a .txt file, there is no GAL to access, there is no
alternate Dist. List to draw the members from, etc.
 
K

Ken Slovak - [MVP - Outlook]

What is your current code for that? Are you able to create the DL but not
fill it, or are you not able to create the DL at all?

In general you would use either:
Outlook.Application.CreateItem(Outlook.OlItemType.olDistributionListItem)

or

folderContacts.Items.Add(Outlook.OlItemType.olDistributionListItem)

assuming folderContacts was an instantiated Contacts folder object.

To add a member to the DL you would use the AddMember() method, which takes
a Recipient object. You would create a Recipient using
NameSpace.CreateRecipient, supplying the email address if the recipient
isn't in an address list or the name if it is..
 
N

NateG

Here's the code I've worked up so far, but it's not adding the members as I
need them. I've already checked the arrays and they are fine and everything
is in correct order:

Private Sub addList(ByVal listName As String, ByVal memberList() As String,
ByVal memberEmails() As String)
MessageBox.Show("Inside addList function") 'To make sure the routine
is being called
Dim oApp As Outlook.Application
Dim oNS As Outlook.NameSpace
Dim oDL As Outlook.DistListItem
Dim i As Integer
Dim oRecipient As Outlook.Recipient

oApp = CreateObject("Outlook.Application")
oNS = oApp.GetNamespace("MAPI")
oDL = oApp.CreateItem(Outlook.OlItemType.olDistributionListItem)
oDL.DLName = listName
oDL.Display() 'Displays the list fine, but can't get the members added

'Pretty sure the problem is here, but not sure the exact syntax of
what I should be doing. memberList is an array of strings containing only
the names of the people in the list. memberEmails is an array of strings
containing "(e-mail address removed)" without the quotes.
For i = 0 To UBound(memberList)
oRecipient = oNS.CreateRecipient(memberList(i))
oRecipient.AddressEntry = memberEmails(i)
oDL.AddMember(oRecipient)
Next
End Sub
 
K

Ken Slovak - [MVP - Outlook]

If the recipient name is in an address list such as Contacts or the Exchange
Global Address List then you can use the name with CreateRecipient(). If not
or if you don't care if you are creating one-off members then use the email
address list to create the recipients:

oRecipient = oNS.CreateRecipient(memberEmails(i))
oRecipient.Resolve()
If (oRecipient.Resolved) Then
oDL.AddMember(oRecipient)
End If

If this code is running outside of Outlook (not in a COM addin) I'd also add
a Logon statement after you create the NameSpace object:

oNS = oApp.GetNamespace("MAPI")
oNS.Logon()

I'd also probably Save the DL after creating it and setting its name and of
course at the end after all the recipients are added.
 
S

Sue Mosher [MVP-Outlook]

If you don't want to create a contact for each entry, try concatenating the name and address:

oRecipient = oNS.CreateRecipient(memberList(i) & "<" & memberEmails(i) & ">")
oRecipent.Resolve
oDL.AddMember(oRecipient)

I have to ask the obvious question: Why are you building a DL in the first place? They're notoriously difficult to maintain. If your goal is to send bulk mail, you don't need a DL for that.
--
Sue Mosher, Outlook MVP
Author of Microsoft Outlook 2007 Programming:
Jumpstart for Power Users and Administrators
http://www.outlookcode.com/article.aspx?id=54
 
N

NateG

Well, I tried the code that Sue recommended:

oRecipient = oNS.CreateRecipient(memberList(i) & "<" & memberEmails(i) & ">")
oRecipient.Resolve
oDL.AddMember(oRecipient)

And this seemed to work somewhat ok for about 2-3 test runs, and then all of
a sudden I started getting a runtime error of:

System.MissingMemberException

At the beginning of the code I do have an Import
Microsoft.Office.Interop.Outlook statement that is underlined in green and
gives a warning every time I build, but it's never caused a runtime error, so
I'm not sure if it's that or not.
 
N

NateG

Well, where to begin....

First, the University that I'm interning at uses a Sun Java e-mail server
(Solaris) and we used to support Eudora, but no longer do.

We also currently support Outlook and use the SJOC (Sun Java Outlook
Connector) to allow people who use Outlook to sync with their e-mails,
calendars, and contacts with the Sun server.

Eudora does not have a way of synchronizing with the Sun server at all, and
a simple Import Contacts from Eudora will import the individual contacts, but
completely skips over the group lists. Some of the people on our campus who
have been using Eudora for years have upwards of 75-100 group lists so
Copy/Paste type of methods to get them into Outlook aren't practical.

Hence, my job (and I'm just an intern there 3rd year programmer and still in
school so I'm kind of lost, don't really know VB as it's like my 4th
language) is to write a program that reads in the text file that Eudora keeps
it's contact list in, parse the names/emails and add them into Outlook. The
individual contacts are working just fine, but when I try to get Eudora's
group contacts into Outlook I assumed that the DL would be the way to go.

If you have any suggestions, I'm wide open, and please take a look at the
error messages I was getting when I replied to Ken's post.
 
S

Sue Mosher [MVP-Outlook]

Call them "group contacts" or call them DLs.It's doesn't matter. They're a real pain regardless -- hard to maintain, prone to inaccuracies, etc. I recommend against using them in general. There are better ways to do bulk mailing if that's their purpose. But given your position, that's not your battle to fight.

--
Sue Mosher, Outlook MVP
Author of Microsoft Outlook 2007 Programming:
Jumpstart for Power Users and Administrators
http://www.outlookcode.com/article.aspx?id=54
 
S

Sue Mosher [MVP-Outlook]

Which code statement raises the error? Did you add a reference to the Microsoft Outlook library to your project? If this is Outlook 2003 or earlier, have you installed the Office PIAs?
 
N

NateG

After setting break points in the code, I discovered the error was causing
the program to crash at the line:

oDL = oApp.CreateItem(Outlook.OlItemType.olDistributionListItem)

I did add the reference to the Outlook library, I think it was 11 but I'll
have to double check. Not sure about the Office PIA's cause I don't know
what that is, and yes it is Office 2003. We won't be upgrading to 2007 until
September '08.
 
N

NateG

I didn't try your exact syntax, but I did find out that the Logon/Logoff was
un-necessary. I did try your recommendation about saving the DL, but as for
the syntax with the Recipient, your's only grabbed the e-mails, not the
member's name, and Sue's grabbed both the Recipient's name as well as their
e-mail address.

I'm going to try a few more things I discovered after doing some research
last night when I get to my desk this morning otherwise, I'm still at a loss.

As I noted to Sue, I did discover that the runtime error was occuring at:

oDL = oApp.CreateItem(Outlook.OlItemType.olDistributionListItem)

And another thing I remember about the error was that it said it occurred in
Microsoft.Visual Basic.dll

Don't know if any of that info is helpful at all.
 
S

Sue Mosher [MVP-Outlook]

PIA = primary interop assembly. In your project references, if the Outlook reference is not pointing to the GAC, then you're going to run into trouble. Rerun Office setup and choose the option to add other components. You'll want to add .NET support for all Office programs. Then, restart your project, remove the original reference and add a new reference to the Outlook library. This time -- since you've installed the PIAs -- it should point properly to the GAC.
--
Sue Mosher, Outlook MVP
Author of Microsoft Outlook 2007 Programming:
Jumpstart for Power Users and Administrators
http://www.outlookcode.com/article.aspx?id=54
 
N

NateG

Ok, got the .NET support installed on Outlook, re-added the reference and
this cleared up all the warnings I was getting, however, I'm still getting
the runtime error at the same line of code. At:

oDL = oApp.CreateItem(Outlook.OlItemType.olDistributionListItem)

I get a runtime error:

A first chance exception of type 'System.MissingMemberException' occurred in
Microsoft.VisualBasic.dll

If I use a Try-Catch block, it won't create the DL at all and completely
skip over the list. Not sure what to try next.
 
S

Sue Mosher [MVP-Outlook]

Does your Imports statement look like this:

Imports Outlook = Microsoft.Office.Interop.Outlook

If not, VB.NET doesn't know what Outlook.OlItemType.olDistributionListItem means. It would, however, know what Microsoft.Office.Interop.Outlook.OlItemType.olDistributionListItem means. See the difference? Try adjusting the Imports statement to the above, so that you can make use of an Outlook namespace to make the code easier to write and read.

--
Sue Mosher, Outlook MVP
Author of Microsoft Outlook 2007 Programming:
Jumpstart for Power Users and Administrators
http://www.outlookcode.com/article.aspx?id=54
 
N

NateG

Yes, heres my Import statements I got these while doing some research on
various websites, and still get the error that crashes the program:

Imports System.IO
Imports Outlook = Microsoft.Office.Interop.Outlook
Imports System.Reflection

Any more thoughts?
 
K

Ken Slovak - [MVP - Outlook]

That members error indicates the class doesn't support that call. So either
there's something wrong with your Outlook reference or your Outlook
Application object isn't correctly instantiated.
 
N

NateG

Wow...

After a couple of days of experimentation, it looks like I finally have it
working!!!

Part of the problem was with the way I was using UBound() and Split to grab
the e-mail addresses/names from the file, another part was that I apparently
needed to save the DL after creating it, every time it added a member in my
for loop, AND at the end of the Sub after everything had been added. I can't
thank you both enough for your help. It's be invaluable!! I'll be posting
the code for the addList() Sub later this morning to see if you guys have any
suggestions, but minus a few coding bugs I need to work out, it looks like
I've got a working program.
 
N

NateG

Sue,

Thanks for holding my hand through the process. With a little
experimentation and trial and error, I was able to get this thing working
finally. I'll post the code that's been working later this morning to see if
you have any further suggestions.

Nate
 

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