Sending whatsapp message from excel vba using selenuim webdriver

1.1k Views Asked by At

When I try sending multiple line message it will send each line separately. How I can send whole message one time. This is my code

Sub WhatsAppMessage()

    'Activate Selenuim web driver
    Dim bot As New WebDriver
    Dim ks As New Keys
    Dim mob As String
    Dim text As String
    
    'Init new Chrome Instance & navigate to webwhatsapp
    bot.Start "Chrome", "https://web.whatsapp.com/"
    bot.Get "/"
    
    'Ask user to scan the QR code. Once Logged in, continue with the macro
    'MsgBox "Please Scan QR code, After you are Logged in, please confirm this message box by clicking OK"
    
    'Determine number of messages by identifying the number of last rows in column A
    LastRow = Cells(Rows.Count, 1).End(xlUp).Row
    
    'Search mobile number / name, Press enter, paste text into whatsapp, press enter to send message
    For i = 4 To LastRow
        'Get mobile number or name from worksheet
        mob = Sheets("Data").Cells(i, 1).Value
        
        'Get Message from Worksheet
        text = Sheets("Data").Cells(2, 1).Value
        
        'click in the searchBox
        bot.FindElementByXPath("//*[@id='side']/div[1]/div/div/div[2]").Click
        
        'wait 500ms
        bot.Wait (500)
        
        'Insert mobile number or name
        bot.SendKeys (mob)
        
        'wait 500ms
        bot.Wait (500)
        
        'press enter to confirm search mobile
        bot.SendKeys (ks.Enter)
        
        'wait 500ms
        bot.Wait (500)
        
        'Load message in to web whatsapp
        bot.SendKeys (text)
        
        'wait 500ms
        bot.Wait (500)
        
        'press enter to send message
        bot.SendKeys (ks.Enter)
        
    Next i
    MsgBox "Done :)"
End Sub

This is the message which I want to send at one time but it will send each line separately

Hello!!
Dear Shree Deeplaxmi Customers,

Exclusive Once in a Year Sale Event - Up to 50% Off! Limited Stock!Join us for an incredible sale starting from July 16, 2023.

Enjoy discounts from 15% to 50% on a wide range of products!

Hurry and take advantage of this limited-time offer. Stock is limited, and once it's gone, the sale ends. Don't miss out on the opportunity to grab your desired items at unbeatable prices.Visit our store soon and make the most of this exclusive discount. 

We look forward to serving you and providing an exceptional shopping experience.See you at our sale event!

Best regards,
Sandeep Sakunde
Shree Deeplaxmi Readymade
2

There are 2 best solutions below

0
DecimalTurn On

For anyone reading this, promise me you won't be using this to spam people, ok? Alright...

I've never used WhatsApp, but it's very common for messaging apps to interpret ENTER as "SUBMIT".

Here, Selenium is just naively imitating what you would do on your keyboard to reproduce the text provided and sending ENTER when it encounters a linebreak. I'm assuming that, like most messaging apps, WhatsApp supports linebreaks inside messages, but you have to press SHIFT+ENTER to avoid sending the message right away.

To do that with Selenium, you could split line-by-line into an array, type the line one at a time and send SHIFT+ENTER at the end of each like. Eg.:

    'Split the text by line
    Dim LinesOfText As Variant
    LinesOfText = Split(SomeText, vbNewLine)
    
    Dim i As Long
    For i = LBound(LinesOfText) To UBound(LinesOfText)
        MyInput.SendKeys LinesOfText(i) 'Type a single line
        If i <> UBound(LinesOfText) Then
            MyInput.SendKeys keyObj.Shift & keyObj.Enter 'New line
        End If
    Next i
    MyInput.SendKeys keyObj.Enter 'Submit form

Here's a the complete demo using jsfiddle:

Sub TextAreaInput()

    Dim MyWebDriver As WebDriver
    Set MyWebDriver = New WebDriver
    MyWebDriver.Start "chrome"
    MyWebDriver.Get "https://jsfiddle.net/decimalturn/ka0zmrfq/8/"
    DoEvents
    
    'This line is needed to get the iframe where the demo html is located
    MyWebDriver.SwitchToFrame MyWebDriver.FindElementByCss("[name='result']")
    
    Dim MyInput As WebElement
    Set MyInput = MyWebDriver.FindElementById("myInput")
    
    Dim keyObj As Selenium.keys
    Set keyObj = New Selenium.keys
    
    'Short text
    MyInput.SendKeys "Some text"
    MyInput.SendKeys keyObj.Shift & keyObj.Enter 'New line
    MyInput.SendKeys "Some text"
    MyInput.SendKeys keyObj.Enter 'Submit form
    
    'Long text
    Dim SomeText As String
    SomeText = "Some text" & vbNewLine & "Some more text" & vbNewLine & "Even more text"
    
    'Split the text by line
    Dim LinesOfText As Variant
    LinesOfText = Split(SomeText, vbNewLine)
    
    Dim i As Long
    For i = LBound(LinesOfText) To UBound(LinesOfText)
        MyInput.SendKeys LinesOfText(i) 'Type a single line
        If i <> UBound(LinesOfText) Then
            MyInput.SendKeys keyObj.Shift & keyObj.Enter 'New line
        End If
    Next i
    MyInput.SendKeys keyObj.Enter 'Submit form
    
    Stop 'Stop execution to look at the result
    MyWebDriver.Close
    Set MyWebDriver = Nothing

End Sub

Note that a simple Replace(text, vbNewLine, keyObj.Shift & keyObj.Enter) won't work because the shift key is sticky, hence, starting from the second line, all your text will be uppercase.

Related questions:

0
sashavb On

DecimalTurn was on the right track, but his code as is doesn't work for me: as he expected "starting from the second line, all your text will be uppercase." So i simply unpressed shift key after the new line and multiline message was built as intended.

MyInput.SendKeys keyObj.Shift & keyObj.Enter 'New line
MyInput.SendKeys keyObj.Shift