Access to Outlook Contacts with ContID

silentwolf

Active member
Local time
Today, 09:28
Joined
Jun 12, 2009
Messages
575
Hi guys,

just wondering what I need to do to be able to get the ContID from Access Database into Outlook Contacts?

I got a code which would transfer an Access Contact at present just from a form as a test to Outlook Contacts that works fine.

But as I like to add all Contacts from Access to Outlook and then work with appointments and so forth in future I need to Access ContID also in Outlook Contacts.

Therefore I created a custom Field in Outlook Named ContID

Code:
   'this code was profidet by a user named database in a https://www.access-o-mania.de/forum/index.php?topic=13596.0
 
   Dim oAppl As Outlook.Application
   Dim oNS As Outlook.Namespace
   Dim oMAPIFolder As Outlook.MAPIFolder
   Dim oContact As Outlook.ContactItem
   Dim strSpezialordner As String
   '----------------------------------
   'Definiert deinen speziellen Ordner
   strSpezialordner = "Spezial"    'DeinOrdnername durch den Namen ersetzen, den du in den Outlook-Kontakten als Unterordner vergeben hast
   '----------------------------------
   On Error GoTo errHandle
 
   If Not IsNull(Me.txtVorname) And Not IsNull(Me.txtNachname) Then
     
       Set oAppl = CreateObject("Outlook.Application")
       Set oNS = oAppl.GetNamespace("MAPI")
     
'       'Den speziellen Ordner zum Beschreiben vorbeitreiten
'       Set oMAPIFolder = oNS.GetDefaultFolder(olFolderContacts).Folders(strSpezialordner)
 
       'Den speziellen Ordner zum Beschreiben vorbeitreiten
       Set oMAPIFolder = oNS.GetDefaultFolder(olFolderContacts)
     
       'Kontakteintrag hinzufügen
       Set oContact = oMAPIFolder.Items.Add
     
    With oContact
        .ContID = Me!txtContID
        .LastName = Me!txtNachname
        .FirstName = Me!txtVorname
        .Save
    End With
       Me!txtContID = Null
       Me!txtNachname = Null
       Me!txtVorname = Null
     
   Else
 
       MsgBox "Es wurden nicht alle erforderlichen Eingaben gemacht!"
       Exit Sub
     
   End If

exitHere:
   Set oContact = Nothing
   Set oMAPIFolder = Nothing
   Set oNS = Nothing
   Set oAppl = Nothing

   Exit Sub
 
errHandle:
   MsgBox "Es ist ein Fehler beim Zugriff auf Outlook aufgetreten! """
   Resume exitHere

it works fine but how would I be able to add a ContID to it it raise an Error when trying to send the ContID of the txtContID

Does the ContID need to be on the Form in Outlook?
And if it does how can I add it to the form?

Or do I need to use the EntryID from Outlook and then refere back to the Database with the same EntryID ?

Hope someone can give me a hint how to overcome this?

Many thanks for your help!!
 
Just curious, what was the error message?
 
Hi,
it went to the Error Message when I add the ContID
without the ContID it runs through nicely
Error Handler
 
Hi,
it went to the Error Message when I add the ContID
without the ContID it runs through nicely
Error Handler
When troubleshooting, you could temporarily disable the error handler, so you can accurately trace the problem.
 
Oh ok,

Sure .. so I did what you suggested and I do get on the line
with oContact
Code:
With oContact
    .ContID = me!txtContID    'Runtime Error 438 Object does not support Methode'


End with
 
Just wondering, does .ContID show up using Intellisense?
 
No not...

but when I do write it in lcase it changes to uCase.
 
I would rather stored the Outlook EntryId with the contact in Access and use that to refer to a Outlook contact.
The EntryId might change if an Outlook item is moved to a different storage container (PST file, or Exchange mailbox), but this also might create problems with custom fields of Outlook items.
 
Hi guys,

I have set up the Database a little different according to a book I am reading in regards to Import and Export Access To Outlook.

tblAnrede(tblSalution) tblKontakte(tblContacts) tblKommunikationsdetails(tblCorrespondencedetails) and tblKommunikationsarten(tblCorrespondenceTyp)

tblKommunikationsdetails is the linked table for tblKontakte and tblKommunikationsarten

Basically .. a Contact has many to many CorrespondenceTypes which is emails, phonenumbers and so on ..
This is as outlook has it in its fields for each contacts...

The issue I have now is how can I export it to Outlook as I have not yet got any contacts in the outlook contacts but don't have a EntryID in my Access Table.
I don't have a EntryID from Outlook as I have at present no Contacts there.
So I am not sure how to Export the Data now to Outlook as this Hex Number is missing.

Hope this is clear what I mean?

Cheers..
 

Attachments

  • DataAccessToOutlook.JPG
    DataAccessToOutlook.JPG
    45.9 KB · Views: 314
  • Kommunikationsarten.JPG
    Kommunikationsarten.JPG
    48.4 KB · Views: 312
  • Kommunikationsdetails.JPG
    Kommunikationsdetails.JPG
    26.7 KB · Views: 287
EntryID is like an Autonumber field in Access, you don't create it, Outlook will.

If you create the Contact in Outlook, you'll be able to import the data with the EntryID into Access.

If you create the Contact in Access, you could try exporting it into Outlook without an EntryID, and Outlook should create it. You can then retrieve the EntryID value and store it in Access.
 
Hi Dbguy,

many thanks for your reply!
Ok I will try that then!

Cheers!

Albert
 
Well I am getting there..

However this is the Code that should send the Data to Outlook.

Code:
Private Sub cmdKontakteExportieren_Click()
    Dim db As DAO.Database
    Dim rstKontakte As DAO.Recordset
    Dim objFolder As Outlook.Folder
    Dim objContactItem As Outlook.ContactItem
    Dim strEntryID As String
    Dim rstKommunikationsdetails As DAO.Recordset
    Dim rst2 As DAO.Recordset2
    Dim fld2 As DAO.Field2
    Set objFolder = GetFolderByPath(Me!txtOrdner)
    Set db = CurrentDb
    Set rstKontakte = db.OpenRecordset("SELECT * FROM tblKontakte", dbOpenDynaset)
    Do While Not rstKontakte.EOF
        Set objContactItem = objFolder.Items.Add(olContactItem)
        With objContactItem
            Select Case rstKontakte!AnredeID
                Case 1
                    .Gender = olMale
                Case 2
                    .Gender = olFemale
            End Select
            .FirstName = rstKontakte!Vorname
            .LastName = rstKontakte!Nachname
            .HomeAddressStreet = rstKontakte!Strasse
            .HomeAddressPostalCode = rstKontakte!PLZ
            .HomeAddressCity = rstKontakte!Ort
            .HomeAddressCountry = rstKontakte!Land
            .Birthday = rstKontakte!Geburtsdatum
            .CompanyName = rstKontakte!Firma
            .BusinessAddressStreet = rstKontakte!FirmaStrasse
            .BusinessAddressPostalCode = rstKontakte!FirmaPLZ
            .BusinessAddressCity = rstKontakte!FirmaOrt
            .BusinessAddressCountry = rstKontakte!FirmaLand
            Set rst2 = rstKontakte!Bild.Value
            Set fld2 = rst2!FileData
            On Error Resume Next
            Kill CurrentProject.Path & "\ContactPicture.jpg"
            On Error GoTo 0
'            fld2.SaveToFile CurrentProject.Path & "\ContactPicture.jpg"
'            .AddPicture CurrentProject.Path & "\ContactPicture.jpg"
            Set rstKommunikationsdetails = db.OpenRecordset("SELECT * FROM qryKommunikationsdetails WHERE KontaktID = " & rstKontakte!kontaktid, dbOpenDynaset)
            Do While Not rstKommunikationsdetails.EOF
                .ItemProperties(rstKommunikationsdetails!KommunikationsartOutlook.Value) = rstKommunikationsdetails!Kommunikationsdetail
                rstKommunikationsdetails.MoveNext
            Loop
            .Save
            strEntryID = .EntryID
            rstKontakte.Edit
            rstKontakte!EntryID = strEntryID
            rstKontakte.Update
        End With
        rstKontakte.MoveNext
    Loop
End Sub

But as a contact could be a Private Person or a Company and there could be of course many Fields that are not field I was wondering what the best way is to change the code to account for that?

Cheers
 
What do you mean by "fields that are not fields?" If you're saying your table has more fields than Outlook has, then try using a query listing only the fields you need to transfer to Outlook for your code instead of using the table.
 
Oh sorry,

If you're saying your table has more fields than Outlook has, then try using a query listing only the fields you need to transfer to Outlook for your code instead of using the table.
Well not exactly

I mean of course where fields are empty in my tblContacts.

So if a Contact is a Private Contact then all Fields for "Firma" are empty and so they are "Null"
and the same is with private Contacts they have different fields which are field or not.

The Code yells when a field is "Null"..
 
Oh sorry,


Well not exactly

I mean of course where fields are empty in my tblContacts.

So if a Contact is a Private Contact then all Fields for "Firma" are empty and so they are "Null"
and the same is with private Contacts they have different fields which are field or not.

The Code yells when a field is "Null"..
What was the error message?
 
It stops right away on the

.FirstName = rstKontakte!Vorname
Runtime Error 94

Nullvalue not allowed or something down that line.

Of course as the field is empty sometimes..
So how can I chatch that for all fields?

Everywhere a Nz function?

But how would it look?

.FirstName=Nz(rstKontakte!Vorname)

:unsure:
 
Hey DBguy,

I did change it and yes it seams to work!
Fantastic many thanks to you!!
It also created a EntryID.. it is 140 characters long is that how long the supose to be?

I should now be able to synchronise if new Entries are made yes?

Something new for me but I slowly getting there :)

Cheers
 

Users who are viewing this thread

Back
Top Bottom