1

I am looking for below code to covert from internet Explorer to Edge browser, request for you help to sort the same.

Sub CHECK_STATUS()
 
    Dim cell As Range
    Dim IntExp As Object
    
    Set IntExp = CreateObject("InternetExplorer.Application")
    IntExp.Visible = False
 
    For Each cell In Range("A2:A20000")
        'Here A2 is cell Address where we have stored urls which we need to test.
        
        If Left(cell.Value, 4) = "http" Then
            
            ' Goto web page
            IntExp.navigate cell.Text
            
           ' Below loop will run until page is fully loaded
            Do While IntExp.Busy Or IntExp.readyState <> 4
                DoEvents
            Loop
 
            ' Now use text which you want to search , error text which you want to compare etc.
            Dim ieDoc As Object
            Set ieDoc = IntExp.document
            
            If ieDoc.getElementsByClassName("box-content").Length <> 0 Then
                cell.Offset(, 1).Value = ieDoc.getElementsByClassName("box-content")(0).innerText 
            End If
        End If
    Next cell
 
    IntExp.Quit
    Set IntExp = Nothing
End Sub
braX
  • 11,506
  • 5
  • 20
  • 33
MIB
  • 35
  • 1
  • 1
  • 6
  • [Related](https://stackoverflow.com/questions/31302304/will-microsoft-edge-support-com-automation-internetexplorer-object). – BigBen Sep 09 '21 at 13:21
  • The Tag is confusing and I am working as entry level with selenium – MIB Sep 09 '21 at 14:01
  • Your current code is not complex and it is fairly intuitive how to convert to selenium basic. I would recommend spending a day or two researching selenium basic and you will be good to go. The tasks you need are 1) instantiate a webdriver instance; 2 ) navigate (`.get`) to an URI; 3) `FindElementsByClass` method call; 4) `.quit` .I suggest you start with [this](https://codingislove.com/browser-automation-in-excel-selenium/) and [these](https://www.youtube.com/results?search_query=wiseowl+selenium) – QHarr Sep 09 '21 at 21:59
  • That will give you more than you need. Your conditional logic will remain the same. You don't need the waits as there are inbuilt implicit waits though you can specify explicit wait times. – QHarr Sep 09 '21 at 22:00
  • Can you pls help me write the code? Want to take value from Cell range "A" for url and print the value / result which required to Cell range "B" – MIB Sep 11 '21 at 13:08

3 Answers3

3

I had been using IE and Internet Object Model (IOM) to achieve automation with internal web-based systems in my works at bank. Since the announcement that IE will be no longer supported by Microsoft at 15 June 2022, I started to look for possible alternative solutions on the internet. After investigation, I found that there are two solutions to achieve automation on Edge browser : 1) SeleniumBasic or 2) Win API. Though SelenimBasic seems to be the mainstream suggestion at forums, Win API can be regarded as better solution in several different ways, especially for my own situations.

Pros of Win API Solution :

  • No need installation and regular update of Edge driver.
  • Able to automate with multiple existing Edge browser windows (which have been opened before program start).
  • Most of codes in existing IOM solution can be preserved and re-applied. It is because both solutions of IOM and Win API should use HTML Document Object Model (DOM) at last to achieve automation on webpage. The difference is on the way to find browser and attain HTMLDocument from browser.

Cons of Win API Solution :

  • We can automate with “webpage” on Edge browser but not the “Edge browser” itself. It is not like IOM and SeleniumBasic that can control web browser. For this, I use Shell function and DOS commands to achieve automation of opening and closing Edge browser.
  • The webpage has to be opened in IE mode at Edge browser which means this solution is subject to Microsoft’s future direction on IE mode of Edge browser.

Sharing on my experiences to use Win API on Edge browser webpage automation :

  1. Place the following codes in a new blank module. I name this module as “MsEdge” usually. The codes in this module are no need to modify for usage. You can directly use the codes even if you don’t know much about Win API.

    Public lngProcessID_Close As Long
    
    'Part 1 --- Locate IES
    
    Private strHwndIES As String
    
    Private lngHwndIndex As Long
    
    Private Declare Function EnumWindows Lib "user32" ( _
        ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
    
    Private Declare Function EnumChildWindows Lib "user32" ( _
        ByVal hWndParent As Long, ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
    
    Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" ( _
        ByVal hWnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
    
    'Part 2 --- Get HTMLDocument from IES
    
    Private Const SMTO_ABORTIFHUNG = &H2
    
    Private Const GUID_IHTMLDocument2 = "{332C4425-26CB-11D0-B483-00C04FD90119}"
    
    Private Declare Function RegisterWindowMessage Lib "user32" Alias "RegisterWindowMessageA" ( _
        ByVal lpString As String) As Long
    
    Private Declare Function SendMessageTimeout Lib "user32" Alias "SendMessageTimeoutA" ( _
        ByVal hWnd As Long, _
        ByVal msg As Long, _
        ByVal wParam As Long, _
        lParam As Any, _
        ByVal fuFlags As Long, _
        ByVal uTimeout As Long, _
        lpdwResult As Long) As Long
    
    Private Declare Function IIDFromString Lib "ole32" ( _
        lpsz As Any, lpiid As Any) As Long
    
    Private Declare Function ObjectFromLresult Lib "oleacc" ( _
        ByVal lResult As Long, _
        riid As Any, _
        ByVal wParam As Long, _
        ppvObject As Any) As Long
    
    'Part 3 --- Check Process Name
    
    Private Declare Function GetWindowThreadProcessId Lib "user32" ( _
        ByVal hWnd As Long, lpdwProcessId As Long) As Long
    
    
    Public Function findEdgeDOM(Title As String, URL As String) As Object
    
        'Find criteria-hitting Edge page in IE mode
    
        Dim hwndIES As Long
    
        Do
    
            hwndIES = enumHwndIES
    
            If hwndIES Then
    
                Set findEdgeDOM = getHTMLDocumentFromIES(hwndIES)
    
                If Not findEdgeDOM Is Nothing Then
    
                    If InStr(findEdgeDOM.Title, Title) * InStr(findEdgeDOM.URL, URL) 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 Long
    
        '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 Long, ByVal lParam As Long) As Boolean
    
        Dim lngProcessID As Long
    
        GetWindowThreadProcessId hWnd, lngProcessID
    
        EnumChildWindows hWnd, AddressOf EnumChildProc, lngProcessID
    
        EnumWindowsProc = True
    
    End Function
    
    Public Function EnumChildProc(ByVal hWnd As Long, ByVal 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 Long) 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 Long) As Object
    
        Dim iid(0 To 3) As Long
    
        Dim lMsg As Long, lRes As Long
    
        lMsg = RegisterWindowMessage("WM_HTML_GETOBJECT")
    
        SendMessageTimeout hWnd, lMsg, 0, 0, SMTO_ABORTIFHUNG, 1000, lRes
    
        If lRes Then
    
            IIDFromString StrPtr(GUID_IHTMLDocument2), iid(0)
    
            ObjectFromLresult lRes, iid(0), 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 Long
    
        Do
    
            hwndIES = enumHwndIES
    
            If hwndIES Then
    
                Set findEdgeDOM = getHTMLDocumentFromIES(hwndIES)
    
                If InStr(findEdgeDOM.Title, Title) * InStr(findEdgeDOM.URL, URL) Then
    
                    Shell "TaskKill /pid " & lngProcessID_Close
    
                    Do
    
                        hwndIES = enumHwndIES
    
                    Loop While hwndIES
    
                    Exit Sub
    
                End If
    
            End If
    
        Loop While hwndIES
    
    End Sub
    
  2. Apply the functions in “MsEdge” module. There are a few application examples for you. Suggest to place and test below codes at another module:

    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("Enter Part of Webpage Title Here", "Enter Part of Webpage URL Here")
        ‘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
    
    End Sub
    
    Sub goEdge()
    
        'Go through every Edge webpage (opened in IE mode) and print out hwndIES, webpage Title & webpage URL
    
        Dim hwndIES As Long
    
        '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 "Input Webpage URL Here"
    
    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"
    
    End Sub
    
2

You need to use SeleniumBasic to automate Edge in VBA. SeleniumBasic is a Selenium based browser automation framework for VB.Net, VBA and VBScript.

I agree with QHarr's comments, you can also follow the steps below to automate Edge browser with SeleniumBasic:

  1. Download the latest version of SeleniumBasic v2.0.9.0 from this link and install it.
  2. Download the corresponding version of Edge WebDriver from this link.
  3. Find the path of SeleniumBasic which is C:\Users\%username%\AppData\Local\SeleniumBasic in my computer (it might also be in this path C:\Program Files\SeleniumBasic), copy the Edge WebDriver msedgedriver.exe to this path.
  4. Rename msedgedriver.exe to edgedriver.exe.
  5. Open Excel and write the VBA code.
  6. In the VBA code interface, click Tools > References, add Selenium Type Library reference and click OK to save.
  7. I write a simple VBA code to show how to automate Edge using SeleniumBasic. You can refer to it and change the code according to your own demands:
Public Sub Selenium()
    For Each cell In Range("A2:A20000")
        Dim bot As New WebDriver
        If Left(cell.Value, 4) = "http" Then
            bot.Start "edge", cell.Value
            bot.Get "/"
            If Not bot.FindElementsByClass("box-content") Is Nothing Then
                cell.Offset(, 1).Value = bot.FindElementsByClass("box-content")(1).Text
            End If
        End If
        bot.Wait 3000
        bot.Quit
    Next cell
End Sub
Yu Zhou
  • 11,532
  • 1
  • 8
  • 22
  • How do I take value from cell range? – MIB Sep 11 '21 at 13:05
  • I want to take the value from A row and print the value in B row – MIB Sep 11 '21 at 13:06
  • @mohammedBaig The functions of taking value and setting value in cells are the same as what you write in IE VBA scripts. For example, if you want to take value form A1 cell, you can use `Range("A1").Value`. If you want to set values in B row, you can use `Cell.Offset(, 1).Value = xxx`. Please check my updated code sample. – Yu Zhou Sep 13 '21 at 07:50
  • Hi @YuZhou, i think you have not got my point, i want to open url from Cell "A" and get classname text and paste Cell B, this programm i want to loop till last entry. – MIB Sep 13 '21 at 11:51
  • @mohammedBaig What I write is just a simple example. I don't know what exact urls you're using, so I can't make a test. I have told you how to do it and you only need to make some changes according to my code. I have told you how to get value from cell A, how to set value in cell B and how to find elements by classname. Please try to edit your code according to my answer. – Yu Zhou Sep 14 '21 at 01:41
  • @mohammedBaig I've managed to change the code according to your original code. Please check the updated code in my answer. – Yu Zhou Sep 14 '21 at 03:05
  • how can I introduce multiple links to process for your reference below are links: https://ok.ru/video/1810319018655 https://ok.ru/video/1807388510879 https://racaty.net/0mwf8fsz54yw https://racaty.net/0y4snx47bgd4 https://racaty.net/1300hs4l556d https://racaty.net/1j8xbp0xtvp1 https://ok.ru/video/2030651443930 https://ok.ru/video/1128245626153 https://ok.ru/video/2547172574672 https://ok.ru/video/1390379797030 – MIB Sep 14 '21 at 20:02
  • I tried to place elsif its not working, its only taking single web, I want to introduce different url with different classname or xpath – MIB Sep 14 '21 at 22:18
  • If the elements you want to search have different class names in every link, I think you can't loop once to get them all. You can only get them one by one. – Yu Zhou Sep 15 '21 at 02:38
  • No other way to find them via loop ? – MIB Sep 15 '21 at 04:06
  • I'm afraid not. – Yu Zhou Sep 15 '21 at 05:10
  • Thank you very much, this helped to run as background process via Task Scheduler. – Ko Nayaki May 03 '22 at 11:10
0

Using a full heavy-weight web browser to do HTTP requests was probably a mistake in the first place, and now is a good opportunity to correct it.

Here is a stackoverflow question on how to do HTTP requests from Excel: it works the same in VBS.

getHTTP with (Excel) VBA?

david
  • 2,435
  • 1
  • 21
  • 33