Private Sub ExportAccessContacts_Click()
Dim OlApp As Object
Dim olContact As Object
On Error GoTo HandleErr
Const olContactItem = 2
Set OlApp = CreateObject("Outlook.Application")
Set olContact = OlApp.CreateItem(olContactItem)
Dim appOutlook As Outlook.Application
Dim nms As Outlook.Namespace
Dim fld As Outlook.MAPIFolder
Dim itm As Object
Dim con As Outlook.ContactItem
Dim lngContactID As Long
Dim ClickResult As VbMsgBoxResultEx
Dim strFile As String
Dim rst As Dao.Recordset
Set rst = CurrentDb.OpenRecordset("q_Contacts_Search", dbOpenSnapshot)
Set appOutlook = GetObject(, "Outlook.Application")
Set nms = appOutlook.GetNamespace("MAPI")
Set fld = nms.GetDefaultFolder(olFolderContacts)
'Move to the first record in the table/query
rst.MoveFirst
'Continue to do the below loop untill all records are either added to Outlook or have been confirmed that they were already there.
Do Until rst.EOF
lngContactID = rst([Name_ID])
strFile = "C:\Users\" & fOSUserName() & "\Pictures\AVC_Backend\Contacts\" & rst("First_Name") & " " & rst("Last_Name") & ".png"
Set con = fld.Items.Find("[CustomerID] = " & lngContactID)
With olContact
.CustomerID = rst("Name_ID")
.FirstName = rst("First_Name")
.MiddleName = Nz(rst("MI"), "")
.LastName = rst("Last_Name")
.FullName = rst("FullName")
.FileAs = rst("FullName")
.Save
End With
'Move to the next record
rst.MoveNext
'Start over again with the next record in the table/query
Loop
MsgBox "Done"
HandleExit:
Exit Sub
HandleErr:
Select Case Err.Number
Case Else
MsgBox Err.Number & vbCrLf & Err.Description
Resume HandleExit
Resume
End Select
Try this, you need to have the lngContactID inside the loop:
Private Sub ExportAccessContacts_Click()
Dim OlApp As Object
Dim olContact As Object
On Error GoTo HandleErr
Const olContactItem = 2
Set OlApp = CreateObject("Outlook.Application")
Set olContact = OlApp.CreateItem(olContactItem)
Dim appOutlook As Outlook.Application
Dim nms As Outlook.Namespace
Dim fld As Outlook.MAPIFolder
Dim itm As Object
Dim con As Outlook.ContactItem
Dim lngContactID As Long
Dim ClickResult As VbMsgBoxResultEx
Dim strFile As String
Dim rst As Dao.Recordset
Set rst = CurrentDb.OpenRecordset("q_Contacts_Search", dbOpenSnapshot)
Set appOutlook = GetObject(, "Outlook.Application")
Set nms = appOutlook.GetNamespace("MAPI")
Set fld = nms.GetDefaultFolder(olFolderContacts)
'Move to the first record in the table/query
rst.MoveFirst
'Continue to do the below loop untill all records are either added to Outlook or have been confirmed that they were already there.
Do Until rst.EOF
lngContactID = rst("Name_ID")
strFile = "C:\Users\" & fOSUserName() & "\Pictures\AVC_Backend\Contacts\" & rst("First_Name") & " " & rst("Last_Name") & ".png"
Set con = fld.Items.Find("[CustomerID] = " & lngContactID)
With olContact
.CustomerID = rst("Name_ID")
.FirstName = Nz(rst("First_Name"), "")
.MiddleName = Nz(rst("MI"), "")
.LastName = rst("Last_Name")
.FullName = rst("FullName")
.FileAs = rst("FullName")
' If IsNull(rst("Work_Anniversary")) = False Then
' .Anniversary = Format(Me.Work_Anniversary, "dd/mm/yyyy")
' End If
' If IsNull(Me.Birthday) = False Then
' .Birthday = Format(Me.Birthday, "mm/dd/yyyy") 'formatted differently so that it exported the medium date format correctly, because I'm not showing the users the year.
' End If
.CompanyName = Nz(DLookup("[Organization]", "[q_Organizations_Search]", "[ORG_Child_ID]=" & Nz(rst("Org_Child_ID"), 0)), "")
' .JobTitle = Nz(Me.JobTitle, "")
' .BusinessAddressStreet = Nz(Me.Address, "")
' .BusinessAddressCity = Nz(Me.City, "")
' .BusinessAddressState = Nz(Me.State, "")
' .BusinessAddressCountry = Nz(DLookup("[Country]", "[t_Country]", "[Country_Auto]=" & Nz(Me.Country, 0)), "")
' .BusinessAddressPostalCode = Nz(Me.ZIP_Postal_Code, "")
' .BusinessTelephoneNumber = Nz(Format(Me.Business_Phone, "(###)###-####"), "")
' .MobileTelephoneNumber = Nz(Format(Me.Mobile_Phone, "(###)###-####"), "")
' .Email1Address = Nz(Me.Email_Address, "")
' .Email2Address = Nz(Me.Other_Email, "")
' 'Need to remove rich text so <DIV and other fomatting don't come over. Need to figure out how to remove extra enters.
' .Body = Nz(PlainText(Notes), "") & vbCrLf & "Skype: " & Nz(Skype, "")
' .WebPage = Nz(Web_Page, "")
' .ManagerName = Nz(DLookup("[FullName]", "[q_Contacts_Search]", "[Name_ID]=" & Nz(Me.Manager_Name_ID, 0)), "")
' .AssistantName = Nz(DLookup("[FullName]", "[q_Contacts_Search]", "[Name_ID]=" & Nz(Me.Assistant_Name_ID, 0)), "")
' If Dir(strFile) <> "" Then
' .AddPicture (strFile)
' End If
.Save
End With
'Move to the next record
rst.MoveNext
'Start over again with the next record in the table/query
Loop
MsgBox "Done"
HandleExit:
Exit Sub
HandleErr:
Select Case Err.Number
Case Else
MsgBox Err.Number & vbCrLf & Err.Description
Resume HandleExit
Resume
End Select
End Sub
Hope this help plus search the forum for outlook or look in similar threads
Private Sub ExportAccessContacts_Click()
'Dim OlApp As Object
Dim olContact As Object
On Error GoTo HandleErr
Const olContactItem = 2
'Set OlApp = CreateObject("Outlook.Application")
Dim appOutlook As Outlook.Application
Dim nms As Outlook.Namespace
Dim fld As Outlook.MAPIFolder
Dim itm As Object
Dim con As Outlook.ContactItem
Dim lngContactID As Long
Dim ClickResult As VbMsgBoxResultEx
Dim strFile As String
Dim rst As Dao.Recordset
Set rst = CurrentDb.OpenRecordset("q_Contacts_Search", dbOpenSnapshot)
Set appOutlook = GetObject(, "Outlook.Application")
Set nms = appOutlook.GetNamespace("MAPI")
Set fld = nms.GetDefaultFolder(olFolderContacts)
'Move to the first record in the table/query
rst.MoveFirst
'Continue to do the below loop untill all records are either added to Outlook or have been confirmed that they were already there.
Do Until rst.EOF
lngContactID = rst("Name_ID")
strFile = "C:\Users\" & fOSUserName() & "\Pictures\AVC_Backend\Contacts\" & rst("First_Name") & " " & rst("Last_Name") & ".png"
Set con = fld.Items.Find("[CustomerID] = " & lngContactID)
Set olContact = OlApp.CreateItem(olContactItem)
With olContact
.CustomerID = rst("Name_ID")
.FirstName = Nz(rst("First_Name"), "")
.MiddleName = Nz(rst("MI"), "")
.LastName = rst("Last_Name")
.FullName = rst("FullName")
.FileAs = rst("FullName")
' If IsNull(rst("Work_Anniversary")) = False Then
' .Anniversary = Format(Me.Work_Anniversary, "dd/mm/yyyy")
' End If
' If IsNull(Me.Birthday) = False Then
' .Birthday = Format(Me.Birthday, "mm/dd/yyyy") 'formatted differently so that it exported the medium date format correctly, because I'm not showing the users the year.
' End If
.CompanyName = Nz(DLookup("[Organization]", "[q_Organizations_Search]", "[ORG_Child_ID]=" & Nz(rst("Org_Child_ID"), 0)), "")
' .JobTitle = Nz(Me.JobTitle, "")
' .BusinessAddressStreet = Nz(Me.Address, "")
' .BusinessAddressCity = Nz(Me.City, "")
' .BusinessAddressState = Nz(Me.State, "")
' .BusinessAddressCountry = Nz(DLookup("[Country]", "[t_Country]", "[Country_Auto]=" & Nz(Me.Country, 0)), "")
' .BusinessAddressPostalCode = Nz(Me.ZIP_Postal_Code, "")
' .BusinessTelephoneNumber = Nz(Format(Me.Business_Phone, "(###)###-####"), "")
' .MobileTelephoneNumber = Nz(Format(Me.Mobile_Phone, "(###)###-####"), "")
' .Email1Address = Nz(Me.Email_Address, "")
' .Email2Address = Nz(Me.Other_Email, "")
' 'Need to remove rich text so <DIV and other fomatting don't come over. Need to figure out how to remove extra enters.
' .Body = Nz(PlainText(Notes), "") & vbCrLf & "Skype: " & Nz(Skype, "")
' .WebPage = Nz(Web_Page, "")
' .ManagerName = Nz(DLookup("[FullName]", "[q_Contacts_Search]", "[Name_ID]=" & Nz(Me.Manager_Name_ID, 0)), "")
' .AssistantName = Nz(DLookup("[FullName]", "[q_Contacts_Search]", "[Name_ID]=" & Nz(Me.Assistant_Name_ID, 0)), "")
' If Dir(strFile) <> "" Then
' .AddPicture (strFile)
' End If
.Save
End With
'Move to the next record
rst.MoveNext
'Start over again with the next record in the table/query
Loop
'clean up
Set olContact =Nothing
Set con=Nothing
Set fld=Nothing
Set nms=Nothing
Set appOutlook =Nothing
rst.close
Set rst=Nothing
MsgBox "Done"
HandleExit:
Exit Sub
HandleErr:
Select Case Err.Number
Case Else
MsgBox Err.Number & vbCrLf & Err.Description
Resume HandleExit
Resume
End Select
End Sub
Set olContact = OlApp.CreateItem(olContactItem)
Sorry, I should have explained what I did, in your code you were using the CreateItem method outside of the recordset loop (Set olContact = OlApp.CreateItem(olContactItem)) so you were overwriting the same object until the very last record.
this one and it appears to be working now.'Dim OlApp As Object
'Set OlApp = CreateObject("Outlook.Application")
StrSQLUpDate = "UPDATE PtblCustomers SET PtblCustomers.InOutlook = True WHERE (((PtblCustomers.InOutlook)=False));"
dbs.Execute (StrSQLUpDate)
I was just going to ask what the con= was forYou need to pay attention and streamline the Outlook side of code a bit. You are declaring and using a couple ofOutlok.Application variables and you set the con variable ( Set con = fld.Items.Find("[CustomerID] = " & lngContactID) ) but you do not do anything with it. I would expect that line or the next to check if con=Nothing (meaning the contactid was not found) then continue to add else GoTo a label ExistingContact: just above the rst.movenext in the loop.
I will be offline for a while (wedding anniversary today ), but see what you can get going and I'll follow up tomorrow if still needed.
Cheers,
Vlad