n3tw0rkadm1n1strat0r
IS-IT--Management
I need a little help with this script...it seems that if a cell is blank, it error's out and stops the script. Also, if the contact already exists it errors out...does anyone know how to fix this?
Code:
Option Explicit
Dim objRootLDAP, objContainer, objContact, objExcel, objSheet
Dim strOU, strPathExcel, intRow, strYourDescription
Dim strContactName, strFirst, strMiddle, strLast, strDep, strHomePhone
Dim strMobilePhone, strFax, strCity, strZip, strState, strEmail, strTitle, strMemberOf
Dim strOfficePhone, strStreet, strCompany, strTitle2, strStreet2, strMailEnable, strAlias, strTargetAddress
strOU = "OU=GP Test ," ' Note the comma
strPathExcel = "C:\Documents and Settings\ekrengel\Desktop\Contact Excel\SampleImportFile.xls"
strYourDescription = "Contact's"
intRow = 2 ' Row 1 contains headings
' Section to bind to Active Directory
Set objRootLDAP = GetObject("LDAP://rootDSE")
Set objContainer = GetObject("LDAP://" & strOU _
& objRootLDAP.Get("DefaultNamingContext"))
' Open the Excel spreadsheet
Set objExcel = CreateObject("Excel.Application")
Set objSheet = objExcel.Workbooks.Open(strPathExcel)
' Here is the loop that cycles through the cells
Do Until (objExcel.Cells(intRow,1).Value) = ""
strContactName = objExcel.Cells(intRow, 1).Value
strFirst = objExcel.cells(intRow, 2).Value
strMiddle = objExcel.cells(intRow, 3).Value
strLast = objExcel.cells(intRow, 4).Value
strDep = objExcel.cells(intRow, 5).Value
strHomePhone = objExcel.cells(intRow, 6).Value
strMobilePhone = objExcel.cells(intRow, 7).Value
strFax = objExcel.cells(intRow, 8).Value
strCity = objExcel.cells(intRow, 9).Value
strZip = objExcel.cells(intRow, 10).Value
strState = objExcel.cells(intRow, 11).Value
strEmail = objExcel.cells(intRow, 12).Value
strTitle = objExcel.cells(intRow, 13).Value
strMemberOf = objExcel.cells(intRow, 14).Value
strOfficePhone = objExcel.cells(intRow, 15).Value
strStreet = objExcel.cells(intRow, 16).Value
strCompany = objExcel.cells(intRow, 17).Value
strTitle2 = objExcel.cells(intRow, 18).Value
strStreet2 = objExcel.cells(intRow, 19).Value
strMailEnable = objExcel.cells(intRow, 20).Value
strAlias = objExcel.cells(intRow, 21).Value
strTargetAddress = objExcel.cells(intRow, 22).Value
Set objContact = objContainer.Create("Contact",_
"cn=" & strContactName)
objContact.Put "givenName", strFirst
objContact.Put "initials", strMiddle
objContact.Put "sn", strLast
objContact.Put "department", strDep
objContact.Put "homePhone", strHomePhone
objContact.Put "mobile", strMobilePhone
objContact.Put "facsimileTelephoneNumber", strFax
objContact.Put "l", strCity
objContact.Put "postalCode", strZip
objContact.Put "c", strState
objContact.Put "mail", strEmail
objContact.Put "title", strTitle
objContact.Put "memberOf", strMemberOf
objContact.Put "telephoneNumber", strOfficePhone
objContact.Put "st", strStreet
objContact.Put "company", strCompany
objContact.Put "description", strTitle2
objContact.Put "streetAddress", strStreet2
objContact.Put "EnableExchangeMail", strMailEnable
objContact.Put "mailNickname", strAlias
objContact.Put "targetAddress", strTargetAddress
objContact.SetInfo
intRow = intRow + 1
Loop
objExcel.Quit
msgbox "Contacts are now added!"
WScript.Quit