0

I have the below VBA codes to automate IE, and then extract the figures of the HTML table and populate the data to Excel table. Is it possible to do the same thing by automate Edge Browser? Since my company don't allow us to install any 3rd party application, Selenium is not an option. As I am not too familarize with coding, highly apprecipate if someone can offer some sample codes

Sub sfc_esg_list()


Dim IE As New InternetExplorer
Dim doc As New MSHTML.HTMLDocument

IE.Visible =*emphasized text* True
'use IE browser to navigate SFC website
IE.navigate "https://www.sfc.hk/en/Regulatory-functions/Products/List-of-ESG-funds"
Do
DoEvents
'Application.Wait (Now() + TimeValue("00:00:04"))
    Loop Until IE.readyState = 4
    
    
    Set doc = IE.Document
    Set TRs = doc.getElementsByTagName("tr")
    
    Sheets("ESG list_SFC").Activate
    'copy and paste the ESG fund list from SFC website to sheets<ESG list_SFC>
    With Sheets("ESG list_SFC")
    .Cells.Clear
    
    For Each TR In TRs
        r = r + 1
        For Each Cell In TR.Children
            C = C + 1
            .Cells(r, C).NumberFormat = "@"
            .Cells(r, C) = Cell.innerText
        Next Cell
        C = 0
    Next TR
    End With
    
    IE.Quit
    Set doc = Nothing
    Set IE = Nothing
    
    
    'Save the file
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    'ActiveWorkbook.Save
    End Sub
chan chan
  • 21
  • 3
  • 1
    See https://stackoverflow.com/questions/70619305/automating-edge-browser-using-vba-without-downloading-selenium – Tim Williams Aug 07 '22 at 18:05
  • Or see https://www.codeproject.com/Tips/5307593/Automate-Chrome-Edge-using-VBA – Tim Williams Aug 07 '22 at 18:07
  • This website also uses a JSON Web Service (https://www.sfc.hk/-/media/SFC/js/json/esg.json) which you can hit and pull the data automatically too. There are a few JSON parsers out there for VBA, also, PowerQuery I believe has the ability to connect and parse JSON – Ryan Wildry Aug 07 '22 at 20:54

1 Answers1

0

IE is pretty much dead at this point. I think it should be something like this.

Sub TryMe()


Dim xmlhttp As Object
Set xmlhttp = CreateObject("MSXML2.serverXMLHTTP")
    
Set request = CreateObject("MSXML2.XMLHTTP")

Dim oHtml As HTMLDocument
Dim oElement As Object
Dim htmlText As String

Set oHtml = New HTMLDocument


request.Open "GET", "https://www.sfc.hk/en/Regulatory-functions/Products/List-of-ESG-funds/", False
request.send
oHtml.body.innerHTML = request.responseText


htmlText = oHtml.getElementsByClassName("tablesorter tablesorter-default tablesorterfcd4c178102ad8")(0).outerhtml

With CreateObject("new:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}") 'Clipboard
    .SetText htmlText
    .PutInClipboard
    Sheets(1).Range("A1").Select
    Sheets(1).PasteSpecial Format:="Unicode Text"
End With
    
End Sub

I thought the class name was 'tablesorter tablesorter-default tablesorterfcd4c178102ad8' but it doesn't seem to work, and I'm not sure why. Can you play around with some other class names? When you hit F12, you will see the HTML code behind the page.

ASH
  • 20,759
  • 19
  • 87
  • 200
  • yes, i can do it by IE. It seems IE will die out soon. ....My question is can we extract figures from HTML table to Excel through Edge browser, without 3rd party installation? – chan chan Aug 08 '22 at 14:27
  • @chanchan IE11 desktop application will go out of support and [be retired on June 15, 2022](https://techcommunity.microsoft.com/t5/windows-it-pro-blog/internet-explorer-11-desktop-app-retirement-faq/ba-p/2366549) for certain versions of Windows 10. But Internet Explorer platform (MSHTML/Trident), including WebOC and COM automation will still work. You can still use VBA to automate IE. – Yu Zhou Aug 09 '22 at 07:00