Extracting Image URLs from HTML Data using VBA Macro



Learn how to automate the process of extracting image URLs from HTML data in Microsoft Excel using a VBA macro. This quick and efficient solution will save you time and effort when dealing with image-rich content.

VBA Code:


Sub ExtractImageURLs()
    Dim rng As Range
    Dim cell As Range
    Dim htmlDoc As Object
    Dim imageNodes As Object
    Dim imageURLs As String
    Dim outputCell As Range

    ' Define the range you want to extract image URLs from
    Dim startCell As Range
    Dim endCell As Range
    Set startCell = Range("A1")  ' Modify the starting cell as needed
    Set endCell = Cells(Rows.Count, startCell.Column).End(xlUp)
    Set rng = Range(startCell, endCell)

    ' Create a new HTMLDocument object
    Set htmlDoc = CreateObject("htmlfile")

    ' Loop through each cell in the defined range
    For Each cell In rng
        ' Load the HTML content into the HTMLDocument object
        htmlDoc.Open
        htmlDoc.Write "<span style=""color: #007F00;"">" & cell.Value & "</span>"
        htmlDoc.Close

        ' Extract the image URLs
        Set imageNodes = htmlDoc.getElementsByTagName("img")
        imageURLs = ""

        ' Concatenate the image URLs
        For Each img In imageNodes
            imageURLs = imageURLs & img.src & vbNewLine
        Next img

        ' Remove extra characters and assign the URLs back to the cell
        cell.Value = "<span style=""color: #7F0000;"">" & Left(imageURLs, Len(imageURLs) - 1) & "</span>"

        ' Adjust the row height to fit the content if needed
        cell.EntireRow.AutoFit
    Next cell
End Sub


Step 1: Access VBA Editor

Open your Excel workbook and access the VBA editor by pressing ALT + F11.

Step 2: Insert the Macro

Copy and paste the provided VBA macro code into a new module.

Step 3: Define the Range

Specify the range from which you want to extract image URLs.

Step 4: Run the Macro

Close the VBA editor, return to your worksheet, and run the "ExtractImageURLs" macro from the "Macros" window (ALT + F8).

Step 5: Enjoy Automation

Sit back and watch as the macro swiftly extracts image URLs, saving you time and manual effort.

Conclusion:

Automate the process of extracting image URLs from HTML data in Excel using the provided VBA macro. Enhance your productivity and efficiency with this simple solution. Happy extracting! 

Mani Blogs

Welcome to my blog! I am Manender, an ecommerce expert with over 7 years of experience. Throughout my career, I've specialized in bulk data scrapping, become an Excel pro, and offered top- notch IT solutions. My knowledge extends to platforms like Shopify, Magento, Amazon, Flipkart, and more. Join me on this exciting journey as we uncover ecommerce secrets, optimize user experiences, and boost sales. Together, we'll create thriving online businesses in this dynamic digital marketplace. Stay tuned for actionable insights, tips, and tricks to achieve ecommerce excellence! Thank you for being part of this amazing community. Let's thrive in ecommerce!

Post a Comment

Previous Post Next Post