3

I have designed a code that sends a message from excel to WhatsApp to list of contacts, it works fine for single contact but when , more than one contact is added it doesn't move to that contacts instead paste other contact names & messages in the message box of the first contact. Here is the excel view, when I press the button the output which is Whatsapp web page doest work as desired as seen in picture.

enter image description here

I know there is some small bug but any support for resolution of this issue will be highly appreciated. Here is the code

Sub Test()

    Dim text As String
    Dim contact As String
    text = Range("C2").Value
    ActiveWorkbook.FollowHyperlink Address:=" https://web.whatsapp.com/"
    If MsgBox("Is WhatsApp Loaded?" & vbNewLine & vbNewLine & "Press No To Cancel", vbYesNo + vbQuestion + vbSystemModal, "WhatsApp") = vbYes Then
        Fazer (100)
        startrow = 2
        startcol = 2
        Do Until Sheets(1).Cells(startrow, 1) = ""
            contact = Cells(startrow, 1)
            text1 = Sheets(1).Cells(startrow, startcol).Value
            Fazer (3000)
                Call SendKeys("{TAB}", True)
            Fazer (1000)
                Call SendKeys(contact, True)
            Fazer (1000)
                Call SendKeys("~", True)
            Fazer (1000)
                Call SendKeys(text1, True)
            Fazer (1000)
                Call SendKeys("~", True)
            Fazer (1000)
            startrow = startrow + 1
        Loop
    Else
    End If
End Sub

Function Fazer(ByVal Acao As Double)
    Application.Wait (Now() + Acao / 24 / 60 / 60 / 1000)
End Function
    
    
M Faizan Farooq
  • 359
  • 1
  • 4
  • 14

3 Answers3

3

You can also use the WhatsApp API and avoid open the wpp web (In ActiveWorkbook.FollowHyperlink Address:=" https://web.whatsapp.com/"), a lot of "sendkeys" and "wait" methods.

Download:

https://www.whatsapp.com/download

Use the InternetExplorer and API with your phone "loaded", the link of the phonenumber should not have special characters (Check documentation)

See this simple example:

Sub wpp()
    Dim IE As Object
    Set IE = CreateObject("InternetExplorer.Application") 'Create object IE
    IE.navigate "whatsapp://send?phone=5511912341234&text=something" 'Send message "something" to this phone (Brazil)
    Application.Wait Now() + TimeSerial(0, 0, 3) 'ok just one wait and sendkeys :v
    SendKeys "~"
    'IE.Quit 'The navigate already kills the IE
    Set IE = Nothing 'Clear the object
End Sub
  • 2
    I used something similar, but to make it work, I had to change SendKeys "~" to SendKeys "{ENTER}". OBS: A command useful in formatting the message is SendKeys "+{ENTER}", which is the command of SHIFT+ENTER, adding a linebreak to the message. – Erick Oct 15 '21 at 18:38
1

There is no error in the code, the way in which WhatsApp Web works is just differently:

After sending your message in Whatsapp, it needs two Tab keystrokes to get to the field to search for contacts (the first tab will hover over the message you've just sent). This applies for every iteration of your program after the first iteration.

If you send no message or just opened WhatsApp Web (which you probably tested) it will need only one Tab keystroke. This applies for the first iteration of your loop only but is currently included for every iteration.

The easiest way to change this in your script is to add another line in the loop after sending the text to simulate this second necessary keystroke.

Do Until Sheets(1).Cells(startrow, 1) = ""
    contact = Cells(startrow, 1)
    text1 = Sheets(1).Cells(startrow, startcol).Value
    Fazer (3000)
        Call SendKeys("{TAB}", True)
    Fazer (1000)
        Call SendKeys(contact, True)
    Fazer (1000)
        Call SendKeys("~", True)
    Fazer (1000)
        Call SendKeys(text1, True)
    Fazer (1000)
        Call SendKeys("~", True)
    Fazer (1000)
    startrow = startrow + 1
    Fazer (1000)
       Call SendKeys("{TAB}", True) 'Simulate the 2nd keystroke
Loop
JulianG
  • 442
  • 3
  • 8
0

By making some changes to the Excel address book template that I created earlier, I created links to web.whatsapp.com for mobile phone numbers - also the mobile numbers are controlled by the macro - . If the number is valid (with the international code at the beginning) and the user has the WhatsApp application installed on their phone, you can send a message even if the number is not stored on your smart phone.

https://www.youtube.com/watch?v=fFQ8Te7U6ys

ActiveWorkbook.FollowHyperlink Address:="https://wa.me/" & ActiveCell.Value & "?text=" & ActiveCell.Offset(0, 4).Value
Application.Wait Now() + TimeSerial(0, 0, 3) 'ok just one wait and sendkeys :v
SendKeys "~"

Review template

kadrleyn
  • 364
  • 1
  • 5