MikePearce
asked on
Keeping Outlook contacts updated from a local/web mysql DB
Hi all,
I have a webserver on the local network which has MYSQL installed.
I was wondering if there was a way I could keep everybodies contacts in the office updated with this database and vice versa, so changes in outlooks contacts are updated when the DB is updated.
Is this possible?
Cheers,
Mike
I have a webserver on the local network which has MYSQL installed.
I was wondering if there was a way I could keep everybodies contacts in the office updated with this database and vice versa, so changes in outlooks contacts are updated when the DB is updated.
Is this possible?
Cheers,
Mike
ASKER
Hey Delta,
If you could post it I would appreciate it, just so I know what I am up against!
I don't know VB all that well, but I am a little familiar with it, so it would be good to see roughly what I need to do.
Thanks for your help delta!
Cheers,
MIKE
If you could post it I would appreciate it, just so I know what I am up against!
I don't know VB all that well, but I am a little familiar with it, so it would be good to see roughly what I need to do.
Thanks for your help delta!
Cheers,
MIKE
The script is between the dashed lines. Sorry but I was on a time crunch when I wrote it originally so the programming is sloppy. At the time I just needed to get it to work, and it does work. We have been using it on the computers at my Job for about a year now and haven't had many problems with it. I also built a program to allow a contact administrator to control what contacts are put into the Database but it is very DB specific and is compiled code so I will not be able to give you an example of that but address can be added directly into the database manually without it. The program just made is so easy that anyone with the program could add contacts.
Here is the Outlook Sync Script:
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- -------
'VARs here are for ADO conn to database
Dim connDB As ADODB.Connection
Dim rsContact As ADODB.Recordset
Const MY_DATABASE = "DSN=GlobalAddress"
Private Sub RemoveContacts() 'This sub removes all contacts from folder
'DIM of Vars
Dim OA As New Outlook.Application ' Outlook Application
Dim ONS As Outlook.NameSpace ' Outlook Namespace.
Dim ContactFolder As MAPIFolder ' Contact folder.
Dim AContact As Outlook.ContactItem ' Contact Item
Dim ErrCount As Integer
On Error GoTo dastart
GoTo damain
dastart: 'This gets skipped unless error happens
On Error Resume Next
ErrCount = 1311 'This could be changed to boolean not sure why I did this
damain: 'Unless error program starts here
'This gets you to the Outlook Mailbox
Set ONS = OA.GetNamespace("MAPI")
'This gets a reference to your Contacts folder
Set ContactFolder = ONS.GetDefaultFolder(olFol derContact s)
'Removing the contacts from the folder
Set AContact = ContactFolder.Items.GetFir st
Do Until ContactFolder.Items.Count = 0
If ContactFolder.Items.Count = 1 Then
ContactFolder.Items.GetFir st
End If
ContactFolder.Items.Remove (1)
ContactFolder.Items.GetNex t
Loop
'All items are now removed
' Always good to clean house and deallocate memory associated with these objects
Set AContact = Nothing
Set ContactFolder = Nothing
Set ONS = Nothing
Set OA = Nothing
If ErrCount = 1311 Then
MsgBox "There may have been an error removing your old contacts... Please contact a network admin to have the problem corrected.", vbExclamation
End If
End Sub
Private Sub AddContacts() 'This sub adds contacts to the folder
'DIM of Vars
Dim OA As New Outlook.Application ' Outlook Application
Dim ONS As Outlook.NameSpace ' Outlook Namespace
Dim ContactFolder As MAPIFolder ' Contact folder
Dim ContactItem1 As Outlook.ContactItem ' Contact Item
On Error GoTo ErrorRtn
' This gets you to the Outlook Mailbox
Set ONS = OA.GetNamespace("MAPI")
' This gets a reference to your Contacts folder
Set ContactFolder = ONS.GetDefaultFolder(olFol derContact s)
' Checking RecordSet Status
If rsContact.State <> adStateOpen Then GoTo ExitRtn
' Move to the first record
rsContact.MoveFirst
While Not rsContact.EOF
Set ContactItem1 = Nothing
' This adds a new contact item to the contact folder
Set ContactItem1 = ContactFolder.Items.Add(ol ContactIte m)
' Now, set whatever contact item values you want to from your database
' I always append an empty string in case the database value is null
'(otherwise you'll get a type mismatch)
ContactItem1.FirstName = rsContact("FirstName").Val ue & ""
ContactItem1.LastName = rsContact("LastName").Valu e & ""
ContactItem1.Title = rsContact("Title").Value & ""
ContactItem1.MiddleName = rsContact("MiddleName").Va lue & ""
ContactItem1.BusinessTelep honeNumber = rsContact("BusinessPhone") .Value & ""
ContactItem1.Business2Tele phoneNumbe r = rsContact("BusinessPhone2" ).Value & ""
ContactItem1.CompanyName = rsContact("Company").Value & ""
ContactItem1.Department = rsContact("Department").Va lue & ""
ContactItem1.JobTitle = rsContact("JobTitle").Valu e & ""
ContactItem1.HomeTelephone Number = rsContact("HomePhone").Val ue & ""
ContactItem1.HomeFaxNumber = rsContact("HomeFax").Value & ""
ContactItem1.MobileTelepho neNumber = rsContact("MobilePhone").V alue & ""
ContactItem1.PagerNumber = rsContact("Pager").Value & ""
ContactItem1.Email1Address = rsContact("EmailAddress"). Value & ""
ContactItem1.Email1Address Type = rsContact("EmailType").Val ue & ""
' Now, save the contact to the Outlook contact list
ContactItem1.Save
' Move to the next database record
rsContact.MoveNext
Wend 'Part of the while loop from above
ExitRtn:
' Always good to clean house and deallocate memory associated with these objects
Set ContactItem1 = Nothing
Set ContactFolder = Nothing
Set ONS = Nothing
Set OA = Nothing
Exit Sub
ErrorRtn: 'This only get used if Error
MsgBox Err.Number & "-" & Err.Description
Resume ExitRtn
' Always good to clean house and deallocate memory associated with these objects
Set ContactItem1 = Nothing
Set ContactFolder = Nothing
Set ONS = Nothing
Set OA = Nothing
Exit Sub
End Sub
Private Sub Application_Startup()
Dim MyFolder As Outlook.MAPIFolder
Dim myNameSpace As Outlook.NameSpace
' This requires that you set Program|References for:
' Microsoft Active Data Objects (v2.5)
' Microsoft Outlook Object Library (v9)
On Error GoTo ErrorHandler
' Create a connection to the database.
Set connDB = CreateObject("ADODB.Connec tion")
'Open Database
With connDB
.ConnectionString = MY_DATABASE
.CursorLocation = adUseClient
.Open
End With
' Open a recordset to the Contact table of the GlobalAddress database
Set rsContact = connDB.Execute("Select * from Contacts")
'Setting info for working with folders
Set myOlApp = CreateObject("Outlook.Appl ication")
Set myNameSpace = myOlApp.GetNamespace("MAPI ")
'Changing error handeling so if folder dosn't exist it doesn't end the program
On Error GoTo addf 'Basically saying if nothing to remove start adding contacts
RemoveContacts 'Created sub to remove contacts
addf: 'Skip around for there being no contacts
On Error GoTo ErrorHandler
AddContacts 'this is a created sub to add contacts
' On the way out, close everything and deallocate their memory
Set myOlApp = Nothing
Set myNameSpace = Nothing
Set rsContact = Nothing
Set connDB = Nothing
Exit Sub
ErrorHandler: 'Only do if there is an error
On Error GoTo endofsub
MsgBox "Papiotrade Contacts could not be updated!", vbExclamation
'close any open files and destroy VARS
Set myOlApp = Nothing
Set myNameSpace = Nothing
connDB.Close
Set connDB = Nothing
If rsContact.State = adStateOpen Then
rsContact.Close
End If
Set rsContact = Nothing
endofsub:
End Sub
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- -------
Hope you can make use of it.
Here is the Outlook Sync Script:
--------------------------
'VARs here are for ADO conn to database
Dim connDB As ADODB.Connection
Dim rsContact As ADODB.Recordset
Const MY_DATABASE = "DSN=GlobalAddress"
Private Sub RemoveContacts() 'This sub removes all contacts from folder
'DIM of Vars
Dim OA As New Outlook.Application ' Outlook Application
Dim ONS As Outlook.NameSpace ' Outlook Namespace.
Dim ContactFolder As MAPIFolder ' Contact folder.
Dim AContact As Outlook.ContactItem ' Contact Item
Dim ErrCount As Integer
On Error GoTo dastart
GoTo damain
dastart: 'This gets skipped unless error happens
On Error Resume Next
ErrCount = 1311 'This could be changed to boolean not sure why I did this
damain: 'Unless error program starts here
'This gets you to the Outlook Mailbox
Set ONS = OA.GetNamespace("MAPI")
'This gets a reference to your Contacts folder
Set ContactFolder = ONS.GetDefaultFolder(olFol
'Removing the contacts from the folder
Set AContact = ContactFolder.Items.GetFir
Do Until ContactFolder.Items.Count = 0
If ContactFolder.Items.Count = 1 Then
ContactFolder.Items.GetFir
End If
ContactFolder.Items.Remove
ContactFolder.Items.GetNex
Loop
'All items are now removed
' Always good to clean house and deallocate memory associated with these objects
Set AContact = Nothing
Set ContactFolder = Nothing
Set ONS = Nothing
Set OA = Nothing
If ErrCount = 1311 Then
MsgBox "There may have been an error removing your old contacts... Please contact a network admin to have the problem corrected.", vbExclamation
End If
End Sub
Private Sub AddContacts() 'This sub adds contacts to the folder
'DIM of Vars
Dim OA As New Outlook.Application ' Outlook Application
Dim ONS As Outlook.NameSpace ' Outlook Namespace
Dim ContactFolder As MAPIFolder ' Contact folder
Dim ContactItem1 As Outlook.ContactItem ' Contact Item
On Error GoTo ErrorRtn
' This gets you to the Outlook Mailbox
Set ONS = OA.GetNamespace("MAPI")
' This gets a reference to your Contacts folder
Set ContactFolder = ONS.GetDefaultFolder(olFol
' Checking RecordSet Status
If rsContact.State <> adStateOpen Then GoTo ExitRtn
' Move to the first record
rsContact.MoveFirst
While Not rsContact.EOF
Set ContactItem1 = Nothing
' This adds a new contact item to the contact folder
Set ContactItem1 = ContactFolder.Items.Add(ol
' Now, set whatever contact item values you want to from your database
' I always append an empty string in case the database value is null
'(otherwise you'll get a type mismatch)
ContactItem1.FirstName = rsContact("FirstName").Val
ContactItem1.LastName = rsContact("LastName").Valu
ContactItem1.Title = rsContact("Title").Value & ""
ContactItem1.MiddleName = rsContact("MiddleName").Va
ContactItem1.BusinessTelep
ContactItem1.Business2Tele
ContactItem1.CompanyName = rsContact("Company").Value
ContactItem1.Department = rsContact("Department").Va
ContactItem1.JobTitle = rsContact("JobTitle").Valu
ContactItem1.HomeTelephone
ContactItem1.HomeFaxNumber
ContactItem1.MobileTelepho
ContactItem1.PagerNumber = rsContact("Pager").Value & ""
ContactItem1.Email1Address
ContactItem1.Email1Address
' Now, save the contact to the Outlook contact list
ContactItem1.Save
' Move to the next database record
rsContact.MoveNext
Wend 'Part of the while loop from above
ExitRtn:
' Always good to clean house and deallocate memory associated with these objects
Set ContactItem1 = Nothing
Set ContactFolder = Nothing
Set ONS = Nothing
Set OA = Nothing
Exit Sub
ErrorRtn: 'This only get used if Error
MsgBox Err.Number & "-" & Err.Description
Resume ExitRtn
' Always good to clean house and deallocate memory associated with these objects
Set ContactItem1 = Nothing
Set ContactFolder = Nothing
Set ONS = Nothing
Set OA = Nothing
Exit Sub
End Sub
Private Sub Application_Startup()
Dim MyFolder As Outlook.MAPIFolder
Dim myNameSpace As Outlook.NameSpace
' This requires that you set Program|References for:
' Microsoft Active Data Objects (v2.5)
' Microsoft Outlook Object Library (v9)
On Error GoTo ErrorHandler
' Create a connection to the database.
Set connDB = CreateObject("ADODB.Connec
'Open Database
With connDB
.ConnectionString = MY_DATABASE
.CursorLocation = adUseClient
.Open
End With
' Open a recordset to the Contact table of the GlobalAddress database
Set rsContact = connDB.Execute("Select * from Contacts")
'Setting info for working with folders
Set myOlApp = CreateObject("Outlook.Appl
Set myNameSpace = myOlApp.GetNamespace("MAPI
'Changing error handeling so if folder dosn't exist it doesn't end the program
On Error GoTo addf 'Basically saying if nothing to remove start adding contacts
RemoveContacts 'Created sub to remove contacts
addf: 'Skip around for there being no contacts
On Error GoTo ErrorHandler
AddContacts 'this is a created sub to add contacts
' On the way out, close everything and deallocate their memory
Set myOlApp = Nothing
Set myNameSpace = Nothing
Set rsContact = Nothing
Set connDB = Nothing
Exit Sub
ErrorHandler: 'Only do if there is an error
On Error GoTo endofsub
MsgBox "Papiotrade Contacts could not be updated!", vbExclamation
'close any open files and destroy VARS
Set myOlApp = Nothing
Set myNameSpace = Nothing
connDB.Close
Set connDB = Nothing
If rsContact.State = adStateOpen Then
rsContact.Close
End If
Set rsContact = Nothing
endofsub:
End Sub
--------------------------
Hope you can make use of it.
ASKER
Thats sweet delta! Thanks!
Is there a way to get it to work the other way around also? So, if someone updates a contact in outlook, it updates the DB?
May be a tall order..
Is there a way to get it to work the other way around also? So, if someone updates a contact in outlook, it updates the DB?
May be a tall order..
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Many, many thanks Delta, you have been a wonderful help.
Have a great weekend!
Mike
Have a great weekend!
Mike
Thanks... You have a great weekend also.
The macro uses an ODBC connection to the database...
The macro runs when Outlook is opened... One ran the macro removes all contacts from the contacts folder sending them to the trash... Then the macro pulls the contacts from the database to Outlook and basically recreats the contacts.... I can post a copy of my script for you to look at or use but it is very difficault to set up unless you have experiance with: ODBC, ADO, VB, Digital Signatures, and Databases...