How to assign the value of "href" attribute of element "a" to a string variable in VBA macro by methods getElementsByClassName and getAttribute RRS feed

  • Question

  • Suppose that the body tag of the HTML file only includes the following element "a" in the source code of the HTML file:

    <a class="fright" href="http://www.microsoft.com">Microsoft</a>

    I want to return the element "a" based on the value of the class attribute by method getElementsByClassName and return the value of the href attribute by method getAttribute and store the value of the href attribute (http://www.microsoft.com) in a string variable.

    I am working on the following code in Microsoft Excel 2013, but this code resulted in error:

    Dim WebSiteURL Dim filePath, strAddress As String Dim IE As Object Set IE = CreateObject("internetexplorer.application") Dim doc As HTMLDocument IE.Visible = False filePath = "C:\Sample.html" IE.navigate filePath Do While IE.readyState <> 4 DoEvents Loop Set doc = IE.document Set WebSiteURL = doc.getElementsByClassName("fright") Set strAddress = WebSiteURL.getAttribute("href")

    Could anyone suggest a sample code having all definitions and declarations of these methods and related variables?

    • Edited by Hamid Shamsiyan Monday, March 9, 2015 7:08 AM
    • Moved by Karen Hu Tuesday, March 10, 2015 2:28 AM VBA issue
    Monday, March 9, 2015 6:58 AM


All replies