MS Office Forum / Outlook / Programming VBA / March 2008
Creating a DistList from a .txt file
|
|
Thread rating:  |
NateG - 26 Mar 2008 19:04 GMT 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.
Ken Slovak - [MVP - Outlook] - 26 Mar 2008 20:35 GMT 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..
 Signature Ken Slovak [MVP - Outlook] http://www.slovaktech.com Author: Professional Programming Outlook 2007 Reminder Manager, Extended Reminders, Attachment Options http://www.slovaktech.com/products.htm
> I'm having problems when trying to generate a dist. list in VB.NET. > [quoted text clipped - 15 lines] > no > alternate Dist. List to draw the members from, etc. NateG - 26 Mar 2008 20:58 GMT 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 "someone@somewhere.com" without the quotes. For i = 0 To UBound(memberList) oRecipient = oNS.CreateRecipient(memberList(i)) oRecipient.AddressEntry = memberEmails(i) oDL.AddMember(oRecipient) Next End Sub
> 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? [quoted text clipped - 32 lines] > > no > > alternate Dist. List to draw the members from, etc. Ken Slovak - [MVP - Outlook] - 26 Mar 2008 21:07 GMT 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.
 Signature Ken Slovak [MVP - Outlook] http://www.slovaktech.com Author: Professional Programming Outlook 2007 Reminder Manager, Extended Reminders, Attachment Options http://www.slovaktech.com/products.htm
> Here's the code I've worked up so far, but it's not adding the members as > I [quoted text clipped - 31 lines] > Next > End Sub NateG - 26 Mar 2008 23:38 GMT 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.
Ken Slovak - [MVP - Outlook] - 27 Mar 2008 14:35 GMT Did you try what I recommended?
 Signature Ken Slovak [MVP - Outlook] http://www.slovaktech.com Author: Professional Programming Outlook 2007 Reminder Manager, Extended Reminders, Attachment Options http://www.slovaktech.com/products.htm
> Well, I tried the code that Sue recommended: > [quoted text clipped - 14 lines] > so > I'm not sure if it's that or not. NateG - 27 Mar 2008 15:41 GMT 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.
> Did you try what I recommended? Ken Slovak - [MVP - Outlook] - 27 Mar 2008 21:51 GMT 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.
 Signature Ken Slovak [MVP - Outlook] http://www.slovaktech.com Author: Professional Programming Outlook 2007 Reminder Manager, Extended Reminders, Attachment Options http://www.slovaktech.com/products.htm
>I didn't try your exact syntax, but I did find out that the Logon/Logoff >was [quoted text clipped - 18 lines] > > Don't know if any of that info is helpful at all. NateG - 28 Mar 2008 15:09 GMT 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.
NateG - 28 Mar 2008 16:25 GMT Ok,
Here's my addList Function that actually works very nicely:
Private Sub addList(ByVal listName As String, ByVal memberList() As String, ByVal memberEmails() As String) 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.Save()
For i = 0 To UBound(memberEmails) If memberList(i) = Nothing Then Exit For End If oRecipient = oNS.CreateRecipient(memberList(i) & "<" & memberEmails(i) & ">") oRecipient.Resolve() oDL.AddMember(oRecipient) oDL.Save() Next
oDL.Save()
'Clean up oApp = Nothing oNS = Nothing oDL = Nothing oRecipient = Nothing
End Sub
Let me know what I need to do to give you guys credit, like forum points or whatever they give you guys for helping out. I'd love to return the favor.
Sue Mosher [MVP-Outlook] - 27 Mar 2008 14:50 GMT 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?
 Signature Sue Mosher, Outlook MVP Author of Microsoft Outlook 2007 Programming: Jumpstart for Power Users and Administrators http://www.outlookcode.com/article.aspx?id=54
> Well, I tried the code that Sue recommended: > [quoted text clipped - 11 lines] > 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. NateG - 27 Mar 2008 15:25 GMT 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.
> 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? > [quoted text clipped - 13 lines] > > 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. Sue Mosher [MVP-Outlook] - 27 Mar 2008 15:46 GMT 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.
 Signature Sue Mosher, Outlook MVP Author of Microsoft Outlook 2007 Programming: Jumpstart for Power Users and Administrators http://www.outlookcode.com/article.aspx?id=54
> After setting break points in the code, I discovered the error was causing > the program to crash at the line: [quoted text clipped - 23 lines] >> > 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. NateG - 27 Mar 2008 17:07 GMT 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.
> 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 [MVP-Outlook] - 27 Mar 2008 17:12 GMT 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.
 Signature Sue Mosher, Outlook MVP Author of Microsoft Outlook 2007 Programming: Jumpstart for Power Users and Administrators http://www.outlookcode.com/article.aspx?id=54
> 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 [quoted text clipped - 11 lines] > >> 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. NateG - 27 Mar 2008 17:21 GMT 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?
> Does your Imports statement look like this: > [quoted text clipped - 17 lines] > > > >> 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 [MVP-Outlook] - 27 Mar 2008 17:37 GMT How are you declaring and instantiating your oApp object variable?
 Signature Sue Mosher, Outlook MVP Author of Microsoft Outlook 2007 Programming: Jumpstart for Power Users and Administrators http://www.outlookcode.com/article.aspx?id=54
> Yes, heres my Import statements I got these while doing some research on > various websites, and still get the error that crashes the program: [quoted text clipped - 26 lines] >> > >> >> 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. NateG - 28 Mar 2008 15:10 GMT 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
NateG - 28 Mar 2008 16:27 GMT I posted the final code as a reply to one of Ken's posts. Thanks for all the help, let me know if there's anything I can do to give you guys more like, forum points, or whatever the reward is for helping out on these forums.
Sue Mosher [MVP-Outlook] - 26 Mar 2008 21:09 GMT 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.
 Signature Sue Mosher, Outlook MVP Author of Microsoft Outlook 2007 Programming: Jumpstart for Power Users and Administrators http://www.outlookcode.com/article.aspx?id=54
> 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 [quoted text clipped - 63 lines] >> > no >> > alternate Dist. List to draw the members from, etc. NateG - 26 Mar 2008 23:52 GMT 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.
> If you don't want to create a contact for each entry, try concatenating the name and address: > [quoted text clipped - 3 lines] > > 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 [MVP-Outlook] - 27 Mar 2008 14:48 GMT 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.
 Signature Sue Mosher, Outlook MVP Author of Microsoft Outlook 2007 Programming: Jumpstart for Power Users and Administrators http://www.outlookcode.com/article.aspx?id=54
> Well, where to begin.... > [quoted text clipped - 28 lines] >> >> 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.
|
|
|