Overview
This article explains how to control Microsoft Edge from Excel VBA to input keywords into a web search form and submit it. In this example, we will use the site https://morinokabu.com/ to demonstrate the process of retrieving form elements, setting values, and submitting data.
Prerequisites
| Item | Details |
| OS | Windows 10 / 11 |
| Excel | Microsoft 365 or Excel 2016+ |
| Additional Tool | SeleniumBasic (VBA Wrapper) |
| Driver | EdgeDriver (msedgedriver.exe) |
| VBA Reference | “Selenium Type Library” |
Setup Steps
- Install SeleniumBasic (Default folder:
C:\SeleniumBasic). - Download the msedgedriver.exe that matches your Edge version and place it in the installation folder.
- In the VBA Editor, go to Tools -> References and check “Selenium Type Library”.
Sample Code
Option Explicit ' Reference: Selenium Type Library is required
Sub SubmitSearchForm()
Dim driver As New Selenium.EdgeDriver
Dim targetURL As String
Dim keywordBox As Selenium.WebElement
Dim submitBtn As Selenium.WebElement
targetURL = "https://morinokabu.com/"
' Launch Edge and load the page
driver.Start
driver.Get targetURL
' Wait for DOM to load completely
Do While driver.ExecuteScript("return document.readyState") <> "complete"
DoEvents
Loop
' Get form elements
Set keywordBox = driver.FindElementById("searchFormKeyword")
Set submitBtn = driver.FindElementById("searchFormSubmit")
' Input keyword
keywordBox.Clear
keywordBox.SendKeys "Excel VBA"
' Submit
submitBtn.Click
' Add next page processing if necessary
' Example: Wait for result page to load
Do While driver.ExecuteScript("return document.readyState") <> "complete"
DoEvents
Loop
' Quit
driver.Quit
End Sub
Code Explanation
| Process | Description |
| FindElementById(“searchFormKeyword”) | Retrieves the input box and uses SendKeys to enter the keyword. |
| FindElementById(“searchFormSubmit”) | Retrieves the submit button and uses Click to submit the form. |
| driver.ExecuteScript(…) | Checks the loading state (“complete”) to wait for the page to load fully. |
Main Methods for Retrieving Elements
| Method | Usage & Features |
| FindElementById(“id”) | The fastest and most reliable method when the ID is unique. |
| FindElementsByName(“name”) | Gets an array of multiple elements sharing a common name attribute. |
| FindElementsByClass(“class”) | Gets a list of elements belonging to the same class. |
| FindElementsByCss(“selector”) | Retrieves elements flexibly using CSS selectors. |
| FindElementsByTag(“tagname”) | Gets elements by tag name (e.g., input, a) in bulk. |
Advanced Applications
- Continuous Posting to Multiple Pages: Manage URLs in an array and loop through them using
For Each. - Dynamic Input Values: Pass cell values or UserForm inputs to
SendKeys. - Result Analysis: Use XPath or CSS selectors to retrieve result elements after submission and write them to an Excel sheet.
- Error Handling: Combine
On Errorstatements withdriver.IsElementPresent()to check if elements exist.
Summary
By using SeleniumBasic and EdgeDriver, you can automate Microsoft Edge form operations directly from Excel VBA.
You can efficiently handle tasks such as searching or posting data by retrieving input boxes and buttons, then using SendKeys and Click. This approach is highly effective for automating data entry into business web systems or performing regular search tasks.
