?
Solved

Compare Outlook Contacts in VB Script

Posted on 2010-01-07
6
Medium Priority
?
562 Views
Last Modified: 2012-05-08
The attached VB script works like a champ but I would like to add one piece of functionality. I would like it to compare the first entry in the local folder to the first entry in the public folder. More specifically, I would be comparing the "first name" values. If they are the same, quit. Otherwise, keep going. Can someone write the code for that part?
Const olFolderContacts = 10
Const olPublicFoldersAllPublicFolders = 18 
Dim olkApp, olkSes, olkContacts, olkPublic, olkContact, intIndex
Set olkApp = CreateObject("Outlook.Application")
Set olkSes = olkApp.GetNamespace("MAPI")
olkSes.Logon "Outlook"
Set olkContacts = olkSes.GetDefaultFolder(olFolderContacts).Folders("TBC Directory")
'Delete all contacts'
For intIndex = olkContacts.Items.count To 1 Step -1
        olkContacts.Items.Remove intIndex
Next

'Copy contacts from the public folder'
'Change the folder name on the next line'
Set olkPublic = olkSes.GetDefaultFolder(olPublicFoldersAllPublicFolders).Folders("TBC Directory").Folders("TBC Directory")
For intIndex = olkPublic.Items.count To 1 Step -1
        Set olkContact = olkPublic.Items(intIndex).Copy
        olkContact.Move olkContacts
Next
Set olkContacts = Nothing
Set olkPublic = Nothing
Set olkContact = Nothing
olkSes.Logoff
Set olkSes = Nothing
Set olkApp = Nothing

Open in new window

0
Comment
Question by:PaulRKrueger
  • 4
  • 2
6 Comments
 
LVL 24

Accepted Solution

by:
purplepomegranite earned 2000 total points
ID: 26208208
The below will compare the first names of the first item in each folder.  If they don't match, the copy process is run.

Const olFolderContacts = 10
Const olPublicFoldersAllPublicFolders = 18 
Dim olkApp, olkSes, olkContacts, olkPublic, olkContact, intIndex
Set olkApp = CreateObject("Outlook.Application")
Set olkSes = olkApp.GetNamespace("MAPI")
olkSes.Logon "Outlook"
Set olkContacts = olkSes.GetDefaultFolder(olFolderContacts).Folders("TBC Directory")
Set olkPublic = olkSes.GetDefaultFolder(olPublicFoldersAllPublicFolders).Folders("TBC Directory").Folders("TBC Directory")

if olkContacts.Items(1).FirstName<>olkPublic.Items(1).FirstName then
	' First names are different...
	
	'Delete all contacts'
	For intIndex = olkContacts.Items.count To 1 Step -1
		olkContacts.Items.Remove intIndex
	Next

	'Copy contacts from the public folder'
	For intIndex = olkPublic.Items.count To 1 Step -1
		Set olkContact = olkPublic.Items(intIndex).Copy
		olkContact.Move olkContacts
	Next
end if

Set olkContacts = Nothing
Set olkPublic = Nothing
Set olkContact = Nothing
olkSes.Logoff
Set olkSes = Nothing
Set olkApp = Nothing

Open in new window

0
 
LVL 4

Author Comment

by:PaulRKrueger
ID: 26209685
Thanks for the quick reply! So, this worked just fine. Unfortunately my logic doesn't. Evidently the contacts aren't sorted in the array in the same order I thought they would be. So, now I need to be more specific in the comparison.

I need the script to find the contact whose first name starts with an asterisk (*) in both folders (there's only one). Then compare those two first names to see if they are the same.

Thanks for the help.
0
 
LVL 4

Author Comment

by:PaulRKrueger
ID: 26209933
Never mind - I got it using this code:



For intIndex = olkContacts.Items.count To 1 Step -1
    olkPV = olkContacts.items(intIndex).lastname
    if inStr(olkPV, "*") > 0 then
        olkPersonalVersion = olkContacts.items(intIndex).lastname
        exit for
    end if
Next

For intIndex = olkPublic.Items.count To 1 Step -1
    olkPV = olkPublic.items(intIndex).lastname
    if inStr(olkPV, "*") > 0 then
        olkPublicVersion = olkPublic.items(intIndex).lastname
        exit for
    end if
Next

if olkPublicVersion <> olkPersonalVersion then
...

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Author Closing Comment

by:PaulRKrueger
ID: 31674224
Quick and effective!
0
 
LVL 4

Author Comment

by:PaulRKrueger
ID: 26210014
I tried to contact you using your email and the site listed in your profile without success. Thanks for your help. I'd like to point you towards my other open question as it may be suited to your skills as well.

http://www.experts-exchange.com/Programming/Languages/Visual_Basic/Q_25027721.html

0
 
LVL 24

Expert Comment

by:purplepomegranite
ID: 26210659
Thanks for the points.  I have actually seen your other question, but haven't posted a reply as the solution would take a little time to implement - and I haven't got the time right now, unfortunately.  If no-one else posts something more useful, I'll take a look when I can - it isn't overly complex to import contacts from a CSV, but it'll take a little code to parse the CSV file and transpose.
0

Featured Post

Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will help to fix the below error for MS Exchange server 2010 I. Out Of office not working II. Certificate error "name on the security certificate is invalid or does not match the name of the site" III. Make Internal URLs and External…
Are you looking for the options available for exporting EDB files to PST? You may be confused as they are different in different Exchange versions. Here, I will discuss some options available.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
Suggested Courses
Course of the Month15 days, 14 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question