Forum Discussion
LM_10
Jun 04, 2023Copper Contributor
update outlook contact with VBA
Hello, I have a list of contacts stored in an Excel file. This Excel file is located in SharePoint. I regularly add or delete contacts (email, name, etc.) in this Excel file. That's why I want to au...
NikolinoDE
Jun 05, 2023Platinum Contributor
To modify the contacts in the "groupe1" contact group instead of the default "Contacts" folder, you need to make some changes to the code.
Here is an updated version of the script (untested) that should update the contacts in the "groupe1" contact group:
Sub UpdateOutlookContactsFromExcelSharePoint()
' SharePoint Configuration
Dim sharepointSiteURL As String
Dim sharepointFilePath As String
Dim excelFileName As String
Dim excelSheetName As String
sharepointSiteURL = "https://your-sharepoint-site-url.com"
sharepointFilePath = "/Shared Documents/Folder/ExcelFile.xlsx"
excelFileName = "ExcelFile.xlsx"
excelSheetName = "Sheet1"
' Outlook Configuration
Dim objOutlook As Object
Dim objNamespace As Object
Dim objContactsFolder As Object
Dim objContactGroup As Object
Dim objContact As Object
' Get a reference to Outlook
Set objOutlook = CreateObject("Outlook.Application")
Set objNamespace = objOutlook.GetNamespace("MAPI")
' Get a reference to the contacts folder
Set objContactsFolder = objNamespace.GetDefaultFolder(10)
' Name of the contact group to access
Dim groupName As String
groupName = "groupe1"
' Find the specified contact group
Set objContactGroup = objContactsFolder.Folders(groupName)
' Open the Excel file in SharePoint
Dim excelApp As Object
Dim excelWorkbook As Object
Dim excelWorksheet As Object
Dim rngData As Object
Dim rngRow As Object
' Create an instance of Excel application
Set excelApp = CreateObject("Excel.Application")
excelApp.Visible = False
excelApp.Workbooks.Open (sharepointSiteURL & sharepointFilePath)
Set excelWorkbook = excelApp.Workbooks(excelFileName)
Set excelWorksheet = excelWorkbook.Worksheets(excelSheetName)
' Get the data range from the Excel sheet
Set rngData = excelWorksheet.Range("A2:C" & excelWorksheet.Cells(excelWorksheet.Rows.Count, 1).End(-4162).Row)
' Loop through the data rows
For Each rngRow In rngData.Rows
Dim strEmailAddress As String
' Get the email address from the cell in column A
strEmailAddress = rngRow.Cells(1).Value
' Check if the email address is not empty
If Not IsEmpty(strEmailAddress) Then
' Find the corresponding contact in the contact group
Set objContact = objContactGroup.Items.Find("[Email1Address] = '" & strEmailAddress & "'")
' Check if the contact is found
If Not objContact Is Nothing Then
' Modify the contact properties with the data from the Excel row
objContact.FirstName = rngRow.Cells(2).Value
objContact.LastName = rngRow.Cells(3).Value
objContact.Email1Address = strEmailAddress
' Save the changes
objContact.Save
End If
End If
Next rngRow
' Close the Excel workbook and quit the Excel application
excelWorkbook.Close SaveChanges:=False
excelApp.Quit
' Clean up the Excel objects
Set rngData = Nothing
Set rngRow = Nothing
Set excelWorksheet = Nothing
Set excelWorkbook = Nothing
Set excelApp = Nothing
' Clean up Outlook objects