Link to home
Start Free TrialLog in
Avatar of golem00
golem00

asked on

Adding a +1 to contact phone numbers in outlook

I'm having trouble writing a script (i used VBS but if you can do it in something else, that's fine too) where I need it to go through all contacts in outlook and check if Mobile, Business and Business2 phone numbers exist and if they do, to add a +1 to the beginning of the number if there isn't already a +1 there. I found some code from Microsoft where someone was trying to add a +1 to fax numbers, but I'm not code savvy enough to jury rig it.

We have this program for our phone system called Mitel YourAssistant where you can drag Outlook contacts into it and directly dial the desk IP phone to call that contact. The problem is that many contacts are long distance so you would have to manually add a 1 to the phone numbers (either in YA for 1 time dial or manually in Outlook for permanent use) in order for it to dial correctly. i tried going through the phone and modem options in the control panel but when i put a 91 to access an outside line for long distance, it doesn't work in YA...so now it just has a 9...

Any ideas, tips or solutions would be greatly appreciated... thanks
Option Explicit
 
Dim start
 
' open file stream
' open outlook data
 
 
'=== Begin Program =============================================================================================================================================
 
start = MsgBox("Do you really wish to run this script? It will modify your Outlook contact numbers by adding a +1 to the beginning of each number. ", vbYesNo, "Outlook Script")
 
If start = "6" Then
	' Check for if contact has mobile, business, or business2 phone numbers
	' Check to see if there already is a +1 at the beginning of the phone number
	' Run PhoneNumberMod
	
End If
 
' outlook VBA expressions:
'	expression.MobileTelephoneNumber
'	expression.BusinessTelephoneNumber
'	expression.Business2TelephoneNumber
 
 
'===============================================================================================================================================================
 
Sub PhoneNumberMod()
   'This will only work on contacts in the CURRENT folder
   Set CurFolder = Application.ActiveExplorer.CurrentFolder
   If CurFolder.DefaultItemType = 2 Then
      MsgBox "This process may take some time. You will be notified when complete.", , "Contact Tools Message"
      Set MyItems = CurFolder.Items
      For i = 1 to MyItems.Count
         Set MyItem = MyItems.Item(i)
         If Left(MyItem.HomeFaxNumber,2) <> "+1" and MyItem.HomeFaxNumber <> "" Then
            MyItem.HomeFaxNumber = "+1" & MyItem.HomeFaxNumber
         End If
         If Left(MyItem.BusinessFaxNumber,2) <> "+1" and MyItem.BusinessFaxNumber <> "" Then
            MyItem.BusinessFaxNumber = "+1" & MyItem.BusinessFaxNumber
         End If
         If Left(MyItem.OtherFaxNumber,2) <> "+1" and MyItem.OtherFaxNumber <> "" Then
            MyItem.OtherFaxNumber = "+1" & MyItem.OtherFaxNumber
         End If
         MyItem.Save
      Next
      MsgBox "Done!", ,"Contact Tools Message"
   Else
      MsgBox "The current folder is not a Contact folder ", , "Contact Tools Message"
   End If
End Sub

Open in new window

Avatar of BrianGEFF719
BrianGEFF719
Flag of United States of America image

What exactly is it doing? I can't test on my outlook (don't wanna possibly hurt my contacts ;)

I would try doing this though:

MyItem.HomeFaxNumber = Replace(MyItem.HomeFaxNumber," ","")
if len(MyItem.HomeFaxNumber) > 7 AND Left(MyItem.HomeFaxNumber,2) <> "+1"
 MyItem.HomeFaxNumber = "+1" & MyItem.HomeFaxNumber
end if
Avatar of golem00
golem00

ASKER

it's basically a skeleton/outline of what i want it to do, but obviously in need of some work.

My main problem is the program as a whole. I can't figure out how to setup VBA to open and modify outlook.

Thanks
ASKER CERTIFIED SOLUTION
Avatar of David Lee
David Lee
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of golem00

ASKER

Thanks BlueDevilFan. I used your code and modified it heavily, but the concept was still the same. Here's what I came up with and it worked. I had to save this as a BAK file and put it in the user's local profile in Outlook. Then I had to select all the contacts in his address book, open the macro menu and run the macro. I'm very far from being a good programmer, so much of what I wrote is probably the most inefficient code ever...lol...
Sub AddOneToContacts()
    Dim switch1, switch2, switch3
    switch1 = 0
    switch2 = 0
    switch3 = 0
        
    Dim olkContact As Outlook.ContactItem
    For Each olkContact In Application.ActiveExplorer.Selection
    
        ' ========== ERROR CHECKING ==========
            If Left(olkContact.BusinessTelephoneNumber, 1) = "1" Or Left(olkContact.BusinessTelephoneNumber, 1) = "0" Or olkContact.BusinessTelephoneNumber = "" Or Left(olkContact.BusinessTelephoneNumber, 1) = "+" Then
                switch1 = 1
            End If
            
            If Left(olkContact.MobileTelephoneNumber, 1) = "1" Or Left(olkContact.BusinessTelephoneNumber, 1) = "0" Or olkContact.MobileTelephoneNumber = "" Or Left(olkContact.MobileTelephoneNumber, 1) = "+" Then
                switch2 = 1
            End If
            
            If Left(olkContact.HomeTelephoneNumber, 1) = "1" Or Left(olkContact.BusinessTelephoneNumber, 1) = "0" Or olkContact.HomeTelephoneNumber = "" Or Left(olkContact.HomeTelephoneNumber, 1) = "+" Then
                switch3 = 1
            End If
 
        
        ' ========== APPLYING MODIFICATION ==========
            If switch1 = 0 Then
                    olkContact.BusinessTelephoneNumber = "1" & olkContact.BusinessTelephoneNumber
                    olkContact.Save
            End If
            
            If switch2 = 0 Then
                    olkContact.MobileTelephoneNumber = "1" & olkContact.MobileTelephoneNumber
                    olkContact.Save
            End If
            
            If switch3 = 0 Then
                    olkContact.HomeTelephoneNumber = "1" & olkContact.HomeTelephoneNumber
                    olkContact.Save
            End If
            
        ' ========== RESETTING SWITCH VARIABLES ==========
            switch1 = 0
            switch2 = 0
            switch3 = 0
            
    Next
    Set olkContact = Nothing
 
End Sub

Open in new window

You''re welcome, golem00.  One question though.  Since you say, "I used your code and modified it heavily, but the concept was still the same.", why don't I deserve any credit for the question?
Avatar of golem00

ASKER

My bad
No problem.  Thanks!