WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

OLE and Outlook
plus

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Create Outlook Distribution List


#DefineFunction CreateDistributionList(distlist,file)
	objApp = ObjectOpen("Outlook.Application")
	objNameSpace = objApp.GetNamespace("MAPI")
	
	if !FileExist(file) then exit
	handle = FileOpen(file,"READ")
	
	;define constants
	olFolderContacts = 10
	olDistributionList = 69 
	olDistributionListItem = 7
	olContact = 40
	olMailItem = 0
	olContactItem = 2
	
	;delete contact list if already present
	objContactFolder = objNameSpace.GetDefaultFolder(olFolderContacts)
	
	objItems = objContactFolder.Items
	For ii = 1 to objItems.count
		objitem = objItems.item(ii)
	   If objitem.class == olDistributionList 
	      myDistListItem = objitem
			dlname = myDistListItem.DLName
	      If StrLower(myDistListItem.DLName) == StrLower(distlist)
	        myDistListItem.Delete() ; ensure starting with new DL
	      EndIf
	   EndIf
	Next
	
	ObjectClose(objContactFolder)
	ObjectClose(objItems)
	
	objContactFolder = objNameSpace.GetDefaultFolder(olFolderContacts)
	
	objDistList = objApp.CreateItem(olDistributionListItem)
	objDistList.DLName = distlist
	
	While @True
		line = FileRead(handle)
		if line == "*EOF*" then break
	   fname = ItemExtract(1,line,",")
		lname = ItemExtract(2,line,",")
		email = ItemExtract(3,line,",")
	   readfullname = StrCat(fname," ",lname)
	   AlreadyThere = @False
	
		objItems = objContactFolder.Items
		count = objItems.count
		For jj = 1 to count
			objItem = objItems.item(jj)
		   If objitem.class == olContact
				fullname = objitem.FullName
		      If StrLower(fullname) == StrLower(readfullname)
		        AlreadyThere = @True
		      EndIf
		   EndIf
			ObjectClose(objItem)
		Next
		
		If !AlreadyThere;Create the new Contact
	      objContact = objApp.CreateItem(olContactItem)
	      objContact.FullName = readfullname
	      objContact.Email1Address = email
	      objContact.Save()
			ObjectClose(objContact)
	   EndIf
	      
	   ;Create the Distribution List item
	   objDistList.DLName = distlist  ; Seems redundant but this line is needed
	   objMailItem = objApp.CreateItem(olMailItem)
	   objRecipients = objMailItem.Recipients
	   objRecipients.Add(email);A Contact with the following e-mail address must exist for the AddMembers method to work
	   objRecipients.ResolveAll
	   objDistList.AddMembers(objRecipients)
	   objDistList.Save() 
		ObjectClose(objRecipients)
	   ObjectClose(objMailItem)
		ObjectClose(objItems)
	EndWhile
	
	FileClose(Handle)
	
	ObjectClose(objDistList)
	ObjectClose(objContactFolder)
	ObjectClose(objNameSpace)
	ObjectClose(objApp)
	return 1
#EndFunction


distlistname = "TestDistlist"
file = "c:\temp\contacts.csv"
CreateDistributionList(distlistname,file)
Message("","Done")

Article ID:   W16091
File Created: 2004:03:30:15:42:48
Last Updated: 2004:03:30:15:42:48