Do you want to build your connection on LinkedIn but are tired of sending messages manually to your all connections?
In this tutorial, we will help you to build a tool in VBA using Selenium WebDriver that can automate sending messages on LinkedIn.
So let’s begin and understand step-by-step how we can send messages on LinkedIn using Selenium in Excel VBA Programming.
Table of Contents
- Create MS Excel Macro-Enable file
- Setup VBA Editor for script
- Create an Open Browser Script
- Create Send Message Script
- Assign Macros to Respected button
- Running Script
Step 1: Create MS Excel Macro-Enable file
- Create a new MS Excel Macro-Enable file with the name LinkedInMessage.xlsm.
- Add LinkedIn profile URL in Column B as shown in the figure.
- Add two buttons from Developer->Insert->Button with the name Open Browser, Send Message.
![](https://kushalstudy.com/wp-content/uploads/2023/04/excel_screenshot.jpg)
Step 2: Setup VBA Editor for script
- Open the VBA editor by pressing Alt+F11 or Developer->Visual Basic.
- In VBA Editor, insert a new module from the menu Insert->Module. New module Module1 will be added to your VBA Project.
- Add reference for Selenium by menu Tools->References… and choose Selenium Type Library.
Step 3: Create an Open Browser Script
- First, we need to create a script that will open the browser. You can copy the open browser script from How to Install Selenium Webdriver for VBA in Windows.
- Copy the code and paste it into the newly created Module1.
Step 4: Create Send Message Script
Add the below code in the newly created Module1 just after the previous code.
Sub SendMessage()
On Error GoTo ErrorHandler
Dim i As Integer, startRow As Integer, endRow As Integer
Dim linkedInURL As String, messageText As String
startRow = 2
endRow = 5000
Application.DisplayStatusBar = True
'''Read Excel file
For i = startRow To endRow
linkedInURL = ""
''Get Linkedin profile URL form column B
linkedInURL = Sheets("LinkedIn").Range("B" & i).Value
''Get Message for sending from Column C
messageText = Sheets("LinkedIn").Range("B" & i).Value
''If there is no LinkedIn URL then exit script
If InStr(linkedInURL, "linkedin") = 0 Then
Exit For
End If
Dim messageButton As Object, messageBox As Object, messageSendButton As Object
' Open current profile
driver.Get linkedInURL
Application.Wait Now + #12:00:03 AM#
Application.StatusBar = "Running: " & linkedInURL
If driver.IsElementPresent(By.XPath("//a[contains(@class, 'message-anywhere-button')]")) = True Then
'' Click on Message button
Set messageButton = driver.FindElementByXPath("//a[contains(@class, 'message-anywhere-button')]")
driver.ExecuteScript "arguments[0].click();", messageButton
Application.Wait Now + #12:00:02 AM#
''Enter message in the message box
Set messageBox = driver.FindElementByXPath("//div[contains(@class, 'msg-form__contenteditable')]")
messageBox.SendKeys messageText
Application.Wait Now + #12:00:02 AM#
''Click on the Send button
Set messageSendButton = driver.FindElementByXPath("//button[contains(@class, 'msg-form__send-button')]")
driver.ExecuteScript "arguments[0].click();", messageSendButton
Application.Wait Now + #12:00:02 AM#
End If
Next i
Application.StatusBar = ""
MsgBox "All Messages are sent successfully!", vbInformation
Exit Sub
ErrorHandler:
MsgBox Err.Description, vbCritical
Application.StatusBar = ""
End Sub
Step 5: Assign Macros to Respected buttons
- Close VBA Editor if it is already open.
- Right-click on Open Browser and assign macro OpenBrowser. As well as assign SendMessage to Send Message button.
Step 6: Running Script
- Click on the Open Browser button, it will open Chrome browser. Open LinkedIn and log in with your credentials.
- In Excel paste all LinkedIn profiles in column B to send Messages and Save data.
- Now click on Send Message button. It will read the LinkedIn profile from column B and messages from column C one by one and send messages.
- After all, profiles are complete, the script will show the message All Messages are sent successfully!
Related: See our guide on How to Send Connect Requests on LinkedIn using Selenium VBA for sending automated Connect requests in LinkedIn.