1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| Option Explicit
Private Sub EnvoyerWhatsApp_Click()
Dim IE As Object
Dim ele As Object
Dim Numero As String
Dim Message As String
' Vérifier si un élément est sélectionné dans la ListBox
If ListBox1.ListIndex = -1 Then
MsgBox "Veuillez sélectionner un contact dans la liste.", vbExclamation
Exit Sub
End If
' Récupérer le numéro de téléphone à partir de la quatrième colonne de la ListBox
Numero = ListBox1.List(ListBox1.ListIndex, 3)
' Vérifier si le numéro est au format correct
If Not EstFormatNumero(Numero) Then
MsgBox "Le numéro de téléphone sélectionné n'est pas au format valide.", vbExclamation
Exit Sub
End If
' Récupérer le message à envoyer
Message = Me.MessageTextBox.Value
' Vérifier si un message est saisi
If Message = "" Then
MsgBox "Veuillez saisir un message à envoyer.", vbExclamation
Exit Sub
End If
' Initialiser une instance Internet Explorer
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
' Ouvrir WhatsApp Web
IE.Navigate "https://web.whatsapp.com/send?phone=" & Numero & "&text=" & Message
' Attente pour permettre à la page de se charger
Do While IE.Busy Or IE.readyState <> 4
DoEvents
Loop
' Attente supplémentaire pour s'assurer que la page est complètement chargée
Application.Wait (Now + TimeValue("00:00:02"))
' Envoyer le message en cliquant sur le bouton d'envoi de WhatsApp Web
Set ele = IE.Document.getElementsByClassName("_4sWnG")(0)
ele.Click
' Fermer Internet Explorer
IE.Quit
' Informer l'utilisateur que le message a été envoyé
MsgBox "Message envoyé avec succès à " & Numero & ".", vbInformation
End Sub
Private Function EstFormatNumero(ByVal Numero As String) As Boolean
' Vérifier si le numéro est au format correct (+221 xx xxx xx xx)
Dim Regex As Object
Set Regex = CreateObject("VBScript.RegExp")
With Regex
.Pattern = "^\+221\s\d{2}\s\d{3}\s\d{2}\s\d{2}$"
EstFormatNumero = .Test(Numero)
End With
End Function |
Partager