Fix Outlook Contacts Phone Number "+32" with C#

Posted Wednesday, September 01, 2004 9:46 PM by CumpsD

I own a Sony Ericsson T630 and manage my contacts with Outlook trough Bluetooth.

This has the side side-effect that I finally started adding everyone I might need to contact to my Contacts, before that I just picked an email and used the Reply-To ;)

But there is one thing that bothers me, I store my numbers in international format (eg: +32 50 .....), but Outlook always removes the + in front of it, and I can't find how to change that. And my phone needs the +!

So, let's write a tool to fix this.

First we need a reference to Outlook. So add a reference to 'Microsoft Outlook 11.0 Object Library' (or 10.0, if you use 10.0 leave a comment if this source worked for you!)



When you did this you'll have two new reference, just remove the Office Core... so that you have something like this:



Our application has a very simple logic, where the Main controls it all.

1using System; 
2using System.Text.RegularExpressions;
3using System.Collections;
4using System.IO;
5using System.Diagnostics;
6using Outlook = Microsoft.Office.Interop.Outlook;
7
8namespace FixContacts {
9 class FixContacts {
10 static void Main(string[] args) {
11 // TODO
12 }
13 } /* FixContacts */
14} /* FixContacts */
First we get the folder where our Contacts are stored.
1 //string contactFolder = @"Cumps David\Contacts"; 
2 Console.Write("Contact Folder: ");
3 string contactFolder = Console.ReadLine();
Then we open Outlook and get the folder.
1 Outlook._Application appOutlook = new Outlook.Application(); 
2
3 Outlook.NameSpace olNS = appOutlook.GetNamespace("MAPI");
4 olNS.Logon("", null, null, null);
5
6 Console.WriteLine("Searching Folder...");
7 Outlook.MAPIFolder olContactFolder = GetFolder(contactFolder);
8 Outlook.Items olContactItems = olContactFolder.Items;
When we have the contact items, we loop over everyone of them, checking if there is a number to be fixed.
1 Console.WriteLine("Fixing Phone Numbers..."); 
2 foreach(Outlook.ContactItem olContact in olContactItems) {
3 CheckNumbersToReplace(olContact);
4 }
Finally, we clean everything up.
1 olNS.Logoff(); 
2
3 olContactItems = null;
4 olContactFolder = null;
5 olNS = null;
6 appOutlook = null;
7 Console.WriteLine("Done");
8 Console.ReadLine();
First the GetFolder method. This will get us our folder we specified with a path.

Again we open Outlook. Then we split the path into the corresponding folders and we look for the root folder.
1Outlook._Application appOutlook = new Outlook.Application(); 
2Outlook.NameSpace olNS = appOutlook.GetNamespace("MAPI");
3olNS.Logon("", null, null, null);
4
5Outlook.MAPIFolder olFolder = null;
6folderPath.Replace("/", @"\");
7string[] arrFolders = folderPath.Split('\\');
8
9foreach (Outlook.MAPIFolder olTmpFolder in olNS.Folders) {
10 if (olTmpFolder.Name == arrFolders[0]) {
11 olFolder = olTmpFolder;
12 break;
13 }
14}
When we have found our root folder, we will look for our first folder and when we find it, we replace our parent object by that folder and go looking for the next folder.
1if (olFolder != null) { 
2 for (int i = 1; i < arrFolders.Length; i++) {
3 Outlook.Folders olFolders = olFolder.Folders;
4 olFolder = null;
5
6 foreach (Outlook.MAPIFolder olTmpFolder in olFolders) {
7 if (olTmpFolder.Name == arrFolders[i]) {
8 olFolder = olTmpFolder;
9 break;
10 }
11 }
12 olFolders = null;
13 }
14}
And in the end we return our found folder after cleaning up.
1arrFolders = null; 
2olNS = null;
3appOutlook = null;
4return olFolder;
Now the CheckNumbersToReplace method, this is a simple one.

We create an ArrayList where we will add every phone number in the contact that starts with our contry prefix.
1 private static void CheckNumbersToReplace(Outlook.ContactItem vContact) { 
2 ArrayList phoneNumbers = new ArrayList();
3 string countryPrefix = "32";
Next we check every phone entry if it contains the prefix. (I only show one of them now)
1 if (vContact.HomeTelephoneNumber != null) { 
2 string phoneNumber = vContact.HomeTelephoneNumber;
3 if (phoneNumber.Substring(0, 2) == countryPrefix) { phoneNumbers.Add(phoneNumber); }
4 }
When we looked over all phonenumbers, we will build a new vCard with the correct information in it, that is, our numbers prefixed with a +.
1 if (phoneNumbers.Count > 0) { 
2 string vCardLocation = "";
3 Outlook.MailItem vCardMail = vContact.ForwardAsVcard();
4 Outlook.Attachments vCardArray = vCardMail.Attachments;
5 foreach (Outlook.Attachment vCard in vCardArray) {
6 vCardLocation = Environment.CurrentDirectory + @"\" + vCard.FileName;
7 vCard.SaveAsFile(vCardLocation);
8 foreach (string phoneNumber in phoneNumbers) {
9 FixNumber(vCardLocation, phoneNumber);
10 }
11 }
12 vContact.Delete();
13 Process launchvCard = new Process();
14 launchvCard.StartInfo = new ProcessStartInfo(vCardLocation);
15 launchvCard.Start();
16 launchvCard.WaitForExit();
17 File.Delete(vCardLocation);
18 }
As you can see, we create a temporary vcard for this, and then launch it, you only have to press Save on it, and it's saved. As this is just a personal tool I didn't have the time or requirement to look into automatically saving it. In the end we delete the temporary file.

The FixNumber method is a simple search and replace in the vCard file.
1 private static void FixNumber(string vCardPath, string phoneNumber) { 
2 if (File.Exists(vCardPath)) {
3 StreamReader readvCard = File.OpenText(vCardPath);
4
5 string vCard = readvCard.ReadToEnd();
6 readvCard.Close();
7 vCard = vCard.Replace(phoneNumber, "+" + phoneNumber);
8
9 StreamWriter writevCard = File.CreateText(vCardPath);
10 writevCard.WriteLine(vCard);
11 writevCard.Close();
12 }
13 } /* FixNumber */
And that is our FixContacts utility. It opens the contact dir, scans the contacts, check for phone numbers and fixes them if necessary and pops them up with the correct information, after which you only have to press Save.

Good enough for me, me phone and Outlook playing nicely together with this.

Full sources and executable are uploaded again. Enjoy.
Filed under:

Comments

# re: Fix Outlook Contacts Phone Number "+32" with C#

Tuesday, September 21, 2004 2:56 PM by jackofall

Hi Just tried this with Oulook 2003 and got an exception error?

Thanks

# re: Fix Outlook Contacts Phone Number "+32" with C#

Tuesday, September 21, 2004 2:59 PM by David Cumps

Which error? Tried debugging? Which line was the error when you debugged?

Greets :)

# re: Fix Outlook Contacts Phone Number "+32" with C#

Thursday, March 08, 2007 9:48 AM by Sam

Hey David,

ik ben ook bezig met iets gelijkaardigs in Office. Ik heb echter geen idee waar je die GetFolder method vandaag haalt.

Deze regel: Outlook.MAPIFolder olContactFolder = GetFolder(contactFolder);

# re: Fix Outlook Contacts Phone Number "+32" with C#

Saturday, March 17, 2007 9:11 AM by CumpsD

Download the code, the GetFolder method is just a method I created :)

Greets

# re: Fix Outlook Contacts Phone Number "+32" with C#

Tuesday, September 11, 2007 5:43 AM by Mina

hey mate how do i deploy this on other computers, it works on my computer but thats because i have visual studio, but how about others?

# re: Fix Outlook Contacts Phone Number "+32" with C#

Tuesday, October 09, 2007 12:44 PM by CumpsD

Compile it into an exe, and give them that executable.

# Recover Outlook &raquo; Blog Archiv &raquo; Recover Corrupt Pst File

Wednesday, November 16, 2011 7:46 PM by Recover Outlook » Blog Archiv » Recover Corrupt Pst File

Pingback from  Recover Outlook  &raquo; Blog Archiv   &raquo; Recover Corrupt Pst File

# Recover Outlook &raquo; Blog Archiv &raquo; Windows Error Message

Wednesday, November 16, 2011 9:59 PM by Recover Outlook » Blog Archiv » Windows Error Message

Pingback from  Recover Outlook  &raquo; Blog Archiv   &raquo; Windows Error Message

# Recover Outlook &raquo; Blog Archiv &raquo; Email Recovery Software

Friday, November 18, 2011 3:23 PM by Recover Outlook » Blog Archiv » Email Recovery Software

Pingback from  Recover Outlook  &raquo; Blog Archiv   &raquo; Email Recovery Software

# Recover Outlook &raquo; Blog Archiv &raquo; How To Recover Deleted Data

Pingback from  Recover Outlook  &raquo; Blog Archiv   &raquo; How To Recover Deleted Data

# Recover Outlook &raquo; Blog Archiv &raquo; Pst Files

Friday, November 25, 2011 12:59 AM by Recover Outlook » Blog Archiv » Pst Files

Pingback from  Recover Outlook  &raquo; Blog Archiv   &raquo; Pst Files