excelvbamicrosoft-edge

Open a website based on cell value and select and click button


I am trying to open a website via Edge based on the url in cell I1 of the worksheet, sendkeys tab 19 times (as I can't figure out how to select the website's button otherwise), hit enter, tab again 34 times, and finally hit enter.

The code is showing

ActiveX can't create object.

Sub OpenWebsiteAndNavigate()

    ' Declare variables
    Dim edge As Object
    Dim url As String
    Dim i As Integer
    
    ' Get the URL from cell I1
    url = Range("I1").Value
    
    ' Create a new instance of Microsoft Edge
    Set edge = CreateObject("Microsoft.Edge.Application")
    
    ' Navigate to the URL
    edge.navigate url
    
    ' Wait for the website to load
    Do While edge.Busy Or edge.readyState <> 4
        DoEvents
    Loop
    
    ' Simulate keystrokes to navigate to specific elements on the website
    For i = 1 To 19
        SendKeys "{TAB}"
    Next i
    SendKeys "{ENTER}"
    For i = 1 To 34
        SendKeys "{TAB}"
    Next i
    SendKeys "{ENTER}"
    
    ' Wait for the website to load
    Do While edge.Busy Or edge.readyState <> 4
        DoEvents
    Loop

End Sub

Expected to load the website, cycle tab 19 times and hit enter, cycle 34 times and hit enter again but ActiveX cant create ("Microsoft.Edge.Application").


Solution

  • First of all i suggest you to use, Google Chrome instead of Edge for development purpose. as it is more developer friendy.

    You should download selenium for VBA from below link & install it in your system, make sure to note down the installation location.

    https://github.com/florentbr/SeleniumBasic/releases/latest

    after you have installed SeleniumBasic you can find some guide here "C:\Users\Your Name\AppData\Local\SeleniumBasic\Selenium.chm"

    You might want to try the WebElement.SendKeys Method section in the documentation.

    Then download microsoft edge driver from the below link https://chromedriver.chromium.org/downloads

    (make sure to download the right driver/version for your system) download the driver unzip the exe file and put it in the SeleniumBasic installation folder.

    *** Now in VBA window goto Tools > References > select Selenium Type Library you must do this step.

    Sub OpenWebsiteAndNavigate()
    
        Dim bot As New WebDriver
        Dim element As Selenium.WebElement
    
        bot.Start "chrome"
        bot.Window.Maximize ' to maximize browser window
    
        bot.Get (Range("I1").Value)  ' this is not recomended you should use spacific referance with sheet name or use Range object
    
        bot.FindElementById ("someID") ' to find some thing with ID
        bot.FindElementByXPath("//*[@id='loginBtn']").Click  ' to find some thing with xpath
        bot.ExecuteScript ("some java script")
    
        bot.SendKeys ("someThing") ' to right some thing in web form
    
        'selects drop down
        Set element = bot.FindElementByXPath("//*[@id='ddlModuleModal']")
        element.AsSelect.SelectByValue (110)
    
    End Sub