Formatage N° de téléphone

Formatage N° de téléphone

Messagepar Michael DELIQUE » 28 Mars 2006 à 20:27

Un petite fonction qui permet de formater simplement le numéro de téléphone

Code : Tout sélectionner
Public Function FormatTelephone(wTelephone As String, Byval wSeparator As String) As String
   
   'Déclaration des Variables   
   Dim i As Long
   Dim J As Long
   Dim K As Long
   Dim Separator As String
   Dim Telephone As String
   Dim Char As String
   
   On Error Goto ErreurFormatTelephone
   
   'teste le téléphone passé en paramètre
   If Trim(wTelephone)="" Then
      FormatTelephone = ""
      Exit Function
   End If
   
   'teste le séparateur passé en paramètre
   If Trim(wSeparator)="" Then
      Separator= "-"
   Else
      Separator = wSeparator
   End If
   
   'élimine tous les espaces
   Telephone =  ""
   
   For I=1 To Len(wTelephone)
      Char = Trim(Cstr(Mid(wTelephone,I,1)))
      If Char Like "[0-9]" Then
         Telephone = Telephone+Trim(Char)
      End If
      Char = ""
   Next
   
   If Trim(Telephone)="" Then
      FormatTelephone = ""
      Exit Function
   End If
   
   FormatTelephone = ""
   
   J=0
   i=0
   
   For I=Len(Telephone) To 1 Step -2
     
      K =(Len(Telephone)-J)
      If  K < 4 Then
         Char = Trim(Left(Telephone, K))
         FormatTelephone = Char+FormatTelephone
         I= 0
      Else
         Char = Trim(Cstr(Mid(Telephone,(I-1),2)))
         FormatTelephone = Separator+Char+FormatTelephone
      End If
      J = J+2
      K = 0
      Char = ""
   Next
   
   J=0
   i=0
   Telephone = ""
   
   Exit Function
ErreurFormatTelephone:
   Msgbox "("+ FormatTelephone)"+Chr(10)+"Erreur " + Str(Err) + " : "+Chr(10) + Cstr(Error)+". "+Chr(10)+"Ligne N° "+Cstr(Erl),16," ERREUR !"
      FormatTelephone  = ""
   Exit Function
End Function
Cordialement

Michael (SMS-Phobique)
----------------------------
"La théorie, c'est quand on sait tout et que rien ne fonctionne. La pratique, c'est quand tout fonctionne et que personne ne sait pourquoi."
Albert EINSTEIN
Avatar de l’utilisateur
Michael DELIQUE
Administrateur
Administrateur
 
Message(s) : 12183
Inscrit(e) le : 16 Déc 2004 à 10:36
Localisation : Paris/Cergy

Retour vers Divers