Edge automation without installing selenium

ballgnm

New Member
Joined
Jan 16, 2023
Messages
11
Office Version
  1. 2021
  2. 2019
  3. 2016
  4. 2013
Platform
  1. Windows
So I heard I can use Edge in IE mode on Windows 10 without installing seleniumbasic.
But couldn't find working script.

In this link Automating Edge Browser using VBA without downloading Selenium
An answer listed possible workarounds.
But still there is no working script.

I merged that registry and written simple webscape script and it still run on Internet Explorer.

Did someone actually managed to make it run on edge browser?

Web Scrape Example
VBA Code:
Sub WebScraping()
    Dim url As String
    Dim IE As Object
    Dim html As Object
    Dim element As Object
    Dim i As Integer
    
    url = "https://www.yahoo.com"
    
    Set IE = CreateObject("InternetExplorer.Application")
    IE.Visible = True
    IE.Navigate url
    
    ' Wait for the page to load completely
    Do While IE.Busy Or IE.ReadyState <> 4
        DoEvents
    Loop
    
    Set html = IE.Document
    i = 1
    For Each element In html.getElementsByClassName("ntk-footer-link")
        Cells(i, 1).Value = element.innerText
        i = i + 1
    Next element

    IE.Quit
    Set IE = Nothing
End Sub
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
On your linked page, I modified the code by Kelvin Yeung:


to run on Excel 2010 and later versions, both 32-bit and 64-bit installations. This involved adding the PtrSafe keyword to the API declarations and using the LongPtr alias for handle and pointer arguments and associated variables in the calling code.

It works for me on Excel 2021 32-bit and 64-bit. Note that you don't navigate with an InternetExplorer object, but instead interact with the HTMLDocument object returned by the findEdgeDOM function, which looks for the specified Edge tab/window which has been opened in Internet Explorer mode.

API code in a standard module:
VBA Code:
Option Explicit

Public lngProcessID_Close As Long

'Part 1 --- Locate IES

Private strHwndIES As String
Private lngHwndIndex As Long

Private Type GUID
    Data1 As Long
    Data2 As Integer
    Data3 As Integer
    Data4(0& To 7&) As Byte
End Type

Private Declare PtrSafe Function EnumWindows Lib "user32.dll" (ByVal lpEnumFunc As LongPtr, ByVal lParam As LongPtr) As Long
Private Declare PtrSafe Function EnumChildWindows Lib "user32.dll" (ByVal hWndParent As LongPtr, ByVal lpEnumFunc As LongPtr, lParam As Long) As Long
Private Declare PtrSafe Function GetClassName Lib "user32.dll" Alias "GetClassNameA" (ByVal hWndptr As LongPtr, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long

'Part 2 --- Get HTMLDocument from IES

Private Declare PtrSafe Function RegisterWindowMessage Lib "user32.dll" Alias "RegisterWindowMessageA" (ByVal lpString As String) As Long
Private Declare PtrSafe Function SendMessageTimeout Lib "user32.dll" Alias "SendMessageTimeoutA" (ByVal hwnd As LongPtr, ByVal msg As Long, ByVal wParam As LongPtr, ByVal lParam As LongPtr, ByVal fuFlags As Long, ByVal uTimeout As Long, lpdwResult As LongPtr) As LongPtr

Private Declare PtrSafe Function IIDFromString Lib "ole32.dll" (ByVal lpsz As LongPtr, lpiid As GUID) As Long
Private Declare PtrSafe Function ObjectFromLresult Lib "oleacc.dll" (ByVal lResult As LongPtr, riid As Any, ByVal wParam As LongPtr, ppvObject As Any) As Long

'Part 3 --- Check Process Name

Private Declare PtrSafe Function GetWindowThreadProcessId Lib "user32.dll" (ByVal hwnd As LongPtr, lpdwProcessId As Long) As Long

Private Const SMTO_ABORTIFHUNG = &H2
Private Const GUID_IHTMLDocument2 = "{332C4425-26CB-11D0-B483-00C04FD90119}"
   

Public Function findEdgeDOM(Title As String, URL As String) As Object

    'Find criteria-hitting Edge page in IE mode

    Dim hwndIES As LongPtr

    Do

        hwndIES = enumHwndIES

        If hwndIES Then

            Set findEdgeDOM = getHTMLDocumentFromIES(hwndIES)

            If Not findEdgeDOM Is Nothing Then

                If InStr(1, findEdgeDOM.Title, Title, vbTextCompare) * InStr(1, findEdgeDOM.URL, URL, vbTextCompare) Then

                    Do

                        hwndIES = enumHwndIES

                    Loop While hwndIES

                    Exit Function

                Else

                    Set findEdgeDOM = Nothing

                End If

            End If

        End If

    Loop While hwndIES

End Function

Public Function enumHwndIES() As LongPtr

    'Get all hwnds of IES

    If Len(strHwndIES) = 0 Then

        EnumWindows AddressOf EnumWindowsProc, 0

        lngHwndIndex = 0

    End If

    'Exit function when overflow

    If lngHwndIndex + 1 > (Len(strHwndIES) - Len(Replace(strHwndIES, ",", ""))) Then

        enumHwndIES = 0

        strHwndIES = ""

        Exit Function

    End If

    'Return IES hwnd one by one

    enumHwndIES = CLng(Split(Left(strHwndIES, Len(strHwndIES) - 1), ",")(lngHwndIndex))

    lngHwndIndex = lngHwndIndex + 1

End Function

Private Function EnumWindowsProc(ByVal hwnd As LongPtr, ByVal lParam As LongPtr) As Boolean
    Dim lngProcessID As Long
   
    GetWindowThreadProcessId hwnd, lngProcessID

    'Note - in accordance with the declarations of EnumChildWindows and EnumChildProc, lngProcessID is passed by reference so that VBA automatically
    'passes the variable's address and dereferences it in EnumChildProc
    EnumChildWindows hwnd, AddressOf EnumChildProc, lngProcessID

    EnumWindowsProc = True

End Function

Public Function EnumChildProc(ByVal hwnd As LongPtr, lParam As Long) As Boolean

    Dim strTargetClass As String, strClassName As String

    strTargetClass = "Internet Explorer_Server"

    strClassName = getClass(hwnd)

    If strClassName = strTargetClass Then

        If GetObject("winmgmts:").ExecQuery("Select Name from Win32_Process WHERE ProcessId='" & lParam & "' AND Name='msedge.exe'").Count Then

            strHwndIES = strHwndIES & hwnd & ","

            lngProcessID_Close = lParam

            EnumChildProc = False

            Exit Function

        End If

    End If

    EnumChildProc = True

End Function

Private Function getClass(hwnd As LongPtr) As String

    Dim strClassName As String
    Dim lngRetLen As Long

    strClassName = Space(255)

    lngRetLen = GetClassName(hwnd, strClassName, Len(strClassName))

    getClass = Left(strClassName, lngRetLen)

End Function

Public Function getHTMLDocumentFromIES(ByVal hwnd As LongPtr) As Object

    Dim iid As GUID
    Dim lMsg As Long, lRes As LongPtr

    lMsg = RegisterWindowMessage("WM_HTML_GETOBJECT")

    SendMessageTimeout hwnd, lMsg, 0, 0, SMTO_ABORTIFHUNG, 1000, lRes

    If lRes Then

        IIDFromString StrPtr(GUID_IHTMLDocument2), iid

        ObjectFromLresult lRes, iid, 0, getHTMLDocumentFromIES

    End If

End Function

Public Sub closeEdge(Title As String, URL As String)

    'Close a Edge browser (the last one in EnumWindows order) with criteria-hitting webpage

    lngProcessID_Close = 0

    Dim findEdgeDOM As Object
    Dim hwndIES As LongPtr
   
    Do

        hwndIES = enumHwndIES

        If hwndIES Then

            Set findEdgeDOM = getHTMLDocumentFromIES(hwndIES)

            If InStr(1, findEdgeDOM.Title, Title, vbTextCompare) * InStr(1, findEdgeDOM.URL, URL, vbTextCompare) Then

                Shell "TaskKill /pid " & lngProcessID_Close

                Do

                    hwndIES = enumHwndIES

                Loop While hwndIES

                Exit Sub

            End If

        End If

    Loop While hwndIES

End Sub

Demo/test code for https://www.mrexcel.com/board/ in another standard module:
VBA Code:
Option Explicit

Sub findEdgeDOM_DemoProc()

    'Demo Proc : Use findEdgeDOM Function to get DOM of specific Edge webpage by Title AND URL

    'Dim docHTML As MSHTML.HTMLDocument     '--- Early Binding

    Dim docHTML As Object                   '--- Late Binding

    Set docHTML = findEdgeDOM("", "https://www.mrexcel.com/board/")
    'You can fill just one argument with either part of webpage title or URL as keyword to search for the target browser and leave another one blank (“”).
    'If you provide both title and URL, the funcitons return DOM of the only browser that meets both criteria.

    If Not docHTML Is Nothing Then
        Debug.Print docHTML.Title, docHTML.URL
        Debug.Print docHTML.Body.innerText
    End If

End Sub


Sub goEdge()

    'Go through every Edge webpage (opened in IE mode) and print out hwndIES, webpage Title & webpage URL

    Dim hwndIES As LongPtr

    'Dim docHTML As MSHTML.HTMLDocument     '--- Early Binding

    Dim docHTML As Object                   '--- Late Binding

    Do

        hwndIES = enumHwndIES

        If hwndIES Then

            Set docHTML = getHTMLDocumentFromIES(hwndIES)

            Debug.Print hwndIES, docHTML.Title, docHTML.URL

        Else

            Debug.Print "Procedure End"

        End If

    Loop While hwndIES

End Sub

Sub openEdgeByURL_DemoProc()

    'Open Edge browser to specific URL

    openEdgeByURL "https://www.mrexcel.com/board/"

End Sub

Public Sub openEdgeByURL(URL As String)

    'Please change the path to your msedge.exe location in your PC

    Shell "C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe -url " & URL, vbNormalFocus

End Sub

Sub closeEdge_DemoProc()

    'Close Edge browser

    'closeEdge "Enter Part of Webpage Title Here", "Enter Part of Webpage URL Here"
    closeEdge "", "https://www.mrexcel.com/board/"

End Sub

Your code:
VBA Code:
Sub WebScraping()
    Dim URL As String
    Dim IE As Object
    Dim html As Object
    Dim element As Object
    Dim i As Integer
   
    URL = "https://www.yahoo.com/"
   
    'You can fill just one argument with either part of webpage title or URL as keyword to search for the target browser and leave another one blank (“”).
    'If you provide both title and URL, the function returns the DOM of the only browser/tab that meets both criteria.
    Set html = findEdgeDOM("", URL)

    If html Is Nothing Then
        Debug.Print "Not found " & URL
        Exit Sub
    End If
   
    Debug.Print html.Title, html.URL
   
    Cells.Clear
    i = 1
    For Each element In html.getElementsByClassName("ntk-footer-link")
        Cells(i, 1).Value = element.innerText
        i = i + 1
    Next element

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,072
Messages
6,122,968
Members
449,095
Latest member
Mr Hughes

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top