Stuck on creating a simple macro I need

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I need a macro to change my Outlook contacts phone numbers in a way that
the first number excecpt the "021" area code, will be repeated. eg if it is
"0218976456" to "02188976456". It's because of new local telephone company
policies for phone numbers and I know nothing of VBA syntaxes. I would be
really grateful if s.b could help me here on how to create such thing.
 
I need a macro to change my Outlook contacts phone numbers in a way that
the first number excecpt the "021" area code, will be repeated. eg if it is
"0218976456" to "02188976456". It's because of new local telephone company
policies for phone numbers and I know nothing of VBA syntaxes. I would be
really grateful if s.b could help me here on how to create such thing.

Simple, eh? The code below do what you want; it assumes the telephone numbers
are formatted "(021) 8x".

Sub PhoneChange()

Dim itmItem As Object
Dim itmItems As Outlook.Items
Dim objProp As Outlook.ItemProperty
Dim blnChanged As Boolean
Dim lngTotal As Long
Dim lngChanged As Long

Set itmItems = ActiveExplorer.CurrentFolder.Items
lngTotal = itmItems.Count
For Each itmItem In itmItems
blnChanged = False
For Each objProp In itmItem.ItemProperties
If InStr(LCase(objProp.Name), "telephone") <> 0 Then
GoSub Phone88
End If
Next objProp
If blnChanged Then
itmItem.Save
lngChanged = lngChanged + 1
End If
Next itmItem
MsgBox "Total items: " & lngTotal & vbCr & "Changed: " & lngChanged, vbInformation + vbOKOnly, "PhoneChange"
Exit Sub

Phone88:
Call Phone88(objProp.Value, itmItem.ItemProperties.Item(objProp.Name), blnChanged)
Return

End Sub

Private Sub Phone88(strIn As String, objOut As ItemProperty, blnChanged As Boolean)

' Changes "(021) 8xxxxxx" to "(021) 88xxxxxx"

Dim lngPos As Long

' Empty or already in "88" scheme?
If strIn = "" Or InStr(strIn, "(021) 88") <> 0 Then
objOut.Value = strIn
blnChanged = blnChanged Or False
Else
lngPos = InStr(strIn, "(021) 8")
If lngPos <> 0 Then
objOut.Value = Left(strIn, lngPos + 6) & "8" & Mid(strIn, lngPos + 7)
blnChanged = True
Debug.Print "Changed " & strIn & " to " & objOut.Value
Else
objOut.Value = strIn
blnChanged = blnChanged Or False
End If
End If
End Sub
 
Thank you Michael, for your Code. But there's a problem I tried to solve. I
don't know how to return back a certain character from the left side.

e.g. from the string "0212243546" I want to first check for the length and
if it is 10 then it is not a right number and the first number after 021
should be repeated e.g 0212245698 to 02122245698 and 0219456321 to
02199456321.
I tried to do this in Phone88 however I just got error messages. I still
need some help… thnx
 
Thank you Michael, for your Code. But there's a problem I tried to solve. I
don't know how to return back a certain character from the left side.

You don't need to. The recognition should be based on the area code,
which has to be in a consistent format (see below). Tell us the format
of your telephone numbers, and we might come up with a solution. BTW,
how many contacts are we talking about?
e.g. from the string "0212243546" I want to first check for the length and
if it is 10 then it is not a right number and the first number after 021
should be repeated e.g 0212245698 to 02122245698 and 0219456321 to
02199456321.

Is that really the format in which the telephone numbers are stored in
your Outlook Contacts? It seems highly unlikely to me. Area codes are
usually surrounded by brackets, a country code with a "+" is often
present, and numbers are often written with a space or a hyphen. E.g.,
most telephone numbers in our Contacts here are formatted:
+cc (aaa) nnnn-nnnn
but there are also all sorts of other formats. To cover them all, a
program would have to border on artificial intelligence for proper
recognition, something which is beyond the casual piece of code thrown
together for a Usenet post.
I tried to do this in Phone88 however I just got error messages. I still
need some help… thnx

My Crystal Ball is in repair; what error messages?

Does the code work for telephone numbers of the format "(021) 8x"?
Michael Bednarek said:
I need a macro to change my Outlook contacts phone numbers in a way that
the first number excecpt the "021" area code, will be repeated. eg if it is
"0218976456" to "02188976456". It's because of new local telephone company
policies for phone numbers and I know nothing of VBA syntaxes. I would be
really grateful if s.b could help me here on how to create such thing.

Simple, eh? The code below do what you want; it assumes the telephone numbers
are formatted "(021) 8x". [snip]
Private Sub Phone88(strIn As String, objOut As ItemProperty, blnChanged As Boolean)

' Changes "(021) 8xxxxxx" to "(021) 88xxxxxx"
[snip]
 
Back
Top