How can create a simple program to modify Windows Address Book and Outlook Address Book entries?
August 13, 2008 7:53 AM   Subscribe

I'd like to create a simple program that would modify the phone numbers in my MS Windows and MS Outlook Address Books (reason is we got new area codes). What is the simplest programming language on windows and best way to get this done by directly accessing the address books (without exporting to CSV)?

I've done some VBS with WMI, Windows batch scripting, perl, python, javascript, PHP.. so I understand programming but not programming with MS Windows DLLs and COMs and APIs etc.

I've done some searching and found it quite complicated to work with VC++ or a similar language on MS Windows so far. Any input would be much appreciated. Thank you very much in advance.
posted by mrbloo to Computers & Internet (8 answers total)
 
Visual Basic for Applications is your best bet.

Here's a good article on how to get started with Outlook VBA.
posted by fusinski at 8:22 AM on August 13, 2008


Visual Basic. You don't need to access dlls for this.
posted by SuperSquirrel at 8:45 AM on August 13, 2008


Best answer: Here's a good start using VBA.
posted by unixrat at 8:55 AM on August 13, 2008


Best answer: I know you said "without exporting to CSV", but if you bring it into Excel through CSV, you just do a global replace of the affected area code, and then import it in again.

Otherwise VBA. Something like this untested macro. Do not use without saving your contacts into another contacts backup folder!

Sub Clean_Contacts()
Dim myContacts As Outlook.MAPIFolder
Dim myContactItems As Outlook.Items
Dim currentContact As Outlook.ContactItem

Dim BadAreaCode As String
Dim NewAreaCode As String

Dim Dirty As Boolean

Dim p As Integer

Const OldNum = "(321)"
Const NewNum = "(456)"

Set myContacts = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderContacts)

Set myContactItems = myContacts.Items


'
' This code assumes all entries are contacts (no distribution lists)
'
For Each currentContact In myContactItems

Dirty = False
p = InStr(currentContact.HomeTelephoneNumber & " ", OldNum)
If p > 0 Then

' Found (321) in tel number at position p
If p > 1 Then
currentContact.HomeTelephoneNumber = Left(currentContact.HomeTelephoneNumber, p - 1) & NewNum & Mid(currentContact.HomeTelephoneNumber, p + Len(OldNum))
Else
currentContact.HomeTelephoneNumber = NewNum & NewNum & Mid(currentContact.HomeTelephoneNumber, p + Len(OldNum))
End If

' Add a category so you can find the banjaxed items if it goes wrong.
currentContact.Categories = currentContact.Categories & ",ex-" & OldNum

Dirty = True

End If


' You might have more conditional logic here. Set Dirty to True if you change the values

If Dirty Then currentContact.Save

Next

End Sub
posted by blue_wardrobe at 1:01 PM on August 13, 2008


Sorry about the lost indentation.
posted by blue_wardrobe at 1:01 PM on August 13, 2008


Best answer: Bother - spotted an error already.

Instead of:

currentContact.HomeTelephoneNumber = NewNum & NewNum & Mid(currentContact.HomeTelephoneNumber, p + Len(OldNum))

it should be:

currentContact.HomeTelephoneNumber = NewNum & Mid(currentContact.HomeTelephoneNumber, p + Len(OldNum))
posted by blue_wardrobe at 1:03 PM on August 13, 2008


Response by poster: Thanks a lot for those! :)
posted by mrbloo at 11:09 PM on August 15, 2008


Best answer: @blue_wardrobe - There were a few errors there, but I fixed them and thanks for that :) It works with Microsoft Outlook.

I also found how to interface with the Windows Address Book (Address book used in Outlook Express) using the open source, free COM located at http://wabaccess.sourceforge.net/ it does exactly what I wanted perfectly!

I just might post a link to the code of the application I with this to help out those who fall upon this page on their searches.

Thanks all once again.
posted by mrbloo at 6:05 AM on August 16, 2008


« Older Looks like I made a wrong turn at Albuquerque...   |   Do I need fillings for receding gums? Newer »
This thread is closed to new comments.