Page 1 sur 1

Formatage N° de téléphone

MessagePublié: 28 Mars 2006 à 20:27
par Michael DELIQUE
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