Hi tony h
I recently found this code at http://www.codeforexcelandoutlook.co...ba-automation/
It does what I want but it writes the contacts to the default Contacts file. I'd like to do one of two things:
Modify the code to add a Contacts file from user input, create the file and then write the records to that file, or
Simply specify in the code the file name that the contacts are to be written to.
Option Explicit
Dim bWeStartedOutlook As Boolean
Sub test()
Dim success As Boolean
success = CreateContactsFromList
End Sub
Function CreateContactsFromList() As Boolean
' creates contacts in bulk from Excel worksheet
' Col A: First Name
' Col B: Last Name
' Col C: Email Address
' Col D: Company Name
' Col E: Business Telephone
' Col F: Business Fax
' Col G: Home Phone
' Row 1 should be a header row
On Error GoTo ErrorHandler
Dim lNumRows As Long
Dim lNumCols As Long
Dim lCount As Long
Dim varContactInfo As Variant
Dim olContact As Object ' Outlook.ContactItem
Dim strCurrentFirstName As String
Dim strCurrentLastName As String
Dim strCurrentEmailAddr As String
Dim strCurrentCompany As String
Dim strCurrentBusinessPhone As String
Dim strCurrentBusinessFax As String
Dim strCurrentHomePhone As String
' figure out how big our array needs to be, and size appropriately
lNumRows = Sheet6.Range(Range("A2"), Range("A" & Rows.Count).End(xlUp)).Count
lNumCols = Sheet6.Range(Range("A1"), Range("IV1").End(xlToLeft)).Count
ReDim varContactInfo(1 To lNumRows, 1 To lNumCols)
varContactInfo = Range(Cells(2, 1), Cells(lNumRows + 1, lNumCols))
' get Outlook
Dim olApp As Object ' Outlook.Application
Set olApp = GetOutlookApp
lCount = 1
Do Until lCount = lNumRows
' assign variant values to intermediate string varbs
strCurrentFirstName = varContactInfo(lCount, 1)
strCurrentLastName = varContactInfo(lCount, 2)
strCurrentEmailAddr = varContactInfo(lCount, 3)
strCurrentCompany = varContactInfo(lCount, 4)
strCurrentBusinessPhone = varContactInfo(lCount, 5)
strCurrentBusinessFax = varContactInfo(lCount, 6)
strCurrentHomePhone = varContactInfo(lCount, 7)
' CreateItem will create a contact in the default folder
Set olContact = olApp.CreateItem(2) ' olContactItem
With olContact
.FirstName = strCurrentFirstName
.LastName = strCurrentLastName
.Email1Address = strCurrentEmailAddr
.CompanyName = strCurrentCompany
.BusinessTelephoneNumber = strCurrentBusinessPhone
.BusinessFaxNumber = strCurrentBusinessFax
.HomeTelephoneNumber = strCurrentHomePhone
End With
olContact.Close olSave
lCount = lCount + 1
Loop
' if we got this far, assume success
CreateContactsFromList = True
GoTo ExitProc
ErrorHandler:
CreateContactsFromList = False
ExitProc:
Set olContact = Nothing
If bWeStartedOutlook Then
olApp.Quit
End If
Set olApp = Nothing
End Function
Function GetOutlookApp() As Object
On Error Resume Next
Set GetOutlookApp = GetObject(, "Outlook.Application")
If Err.Number <> 0 Then
Set GetOutlookApp = CreateObject("Outlook.Application")
bWeStartedOutlook = True
End If
On Error GoTo 0
End Function
I also found this code at http://forums.techguy.org/business-a...nto-new-2.html
Same questions regarding modification.
'Option Explicit
Dim appOutlook As Outlook.Application
Dim objNameSpace As Outlook.Namespace
Dim objContactFolder As Outlook.MAPIFolder
Dim objContacts As Outlook.ContactItem
Dim myDistList As Outlook.DistListItem
Sub DistList()
Set appOutlook = GetObject(, "Outlook.Application")
Set objNameSpace = appOutlook.GetNamespace("MAPI")
Set objContactFolder = objNameSpace.GetDefaultFolder(olFolderContacts)
Set myMailItem = appOutlook.CreateItem(olMailItem)
Set myRecipients = myMailItem.Recipients
Set myDistList = appOutlook.CreateItem(olDistributionListItem)
For i = 2 To Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
Set objContacts = objContactFolder.Items.Add(olContactItem)
With objContacts
If Not Range("I" & i) = "" Then
' .CompanyName = Range("B" & i).Value
.LastName = Range("C" & i).Value
.FirstName = Range("B" & i).Value
.HomeAddressStreet = Range("D" & i).Value
.HomeAddressCity = Range("E" & i).Value
.HomeAddressState = Range("F" & i).Value
.HomeAddressPostalCode = Range("G" & i).Value
' .BusinessAddressCountry = Range("I" & i).Value
.JobTitle = Range("J" & i).Value
.HomeTelephoneNumber = Range("H" & i).Value
' .BusinessFaxNumber = Range("L" & i).Value
.Email1Address = Range("I" & i).Value
.Body = Range("N" & i).Value
.Save
End If
End With
myRecipients.Add (Range("I" & i).Value)
Next
myRecipients.ResolveAll
myDistList.AddMembers myRecipients
myDistList.Display
End Sub
Any ideas?
John
Bookmarks