VBA Web scraping for image src's

messer5740

New Member
Joined
Aug 13, 2020
Messages
2
Office Version
  1. 2013
Platform
  1. Windows
Hi all,

I am looking to make a program which I can web scrape HTML src's with. My code enters info into a couple text boxes and runs an external query to generate graphs. I want to take the graph src, download it, and insert it into my excel doc. I am having a hell of a time getting the src scraped however. Think you can give me a hand?

Here is my code:

VBA Code:
Private Declare PtrSafe Function URLDownloadToFile Lib "urlmon" _
    Alias "URLDownloadToFileA" (ByVal pCaller As Long, ByVal szURL As String, _
    ByVal szFileName As String, ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long


Sub openScrubLineLossCharts()

    Dim ie As Object
    Dim dateToday As Date
    Dim userElement As Variant
    Dim imgsrc As Object
    Dim dlpath As Variant
    Dim previewImg As Variant
    Dim URL As String
    Dim html As HTMLDocument
    Dim ElementCol As Object
    Dim Link As Object
    Dim ecol As Long
    Dim objElement As IHTMLElement
    
    
    URL = "http://botprodweb/LineLossReview/"
    
    Set ie = CreateObject("internetexplorer.application") 'internet explorer
   
    
    
    'ensures website is properly loaded
    With ie
        ie.Visible = True '78687968796867876878976876True when testing, False when active. Allows view of changes made to website
    
        ie.Navigate "http://botprodweb/LineLossReview/default.aspx" 'Line loss query website

        Do While .Busy
            DoEvents
        Loop

        Do While .ReadyState <> 4
            DoEvents
        Loop
    End With

'
' etc
'
'
' BELOW IS WHERE I NEED HELP AT!


    Dim doc As HTMLDocument
    Set doc = ie.Document
    Dim sDD As String
    sDD = doc.getElementsByTagName("img")(src)
    '25 line loss chart
    Dim ActiveDocument As Object
    Set ActiveDocument = ie
    
    'Set objElement = ActiveDocument.all.tags("head").Item(0)
    
   
Dim intCount As Integer

'For Each objElement In ActiveDocument.body.all
    intCount = intCount + 1
    objElement.ID = objElement.tagName & intCount
'Next
 


    
    
        dlpath = "J:\BP&S_Operations\Daily Multi-Tool\Line Loss Chart Photo Dump\"
    URLDownloadToFile 0, src, dlpath & "25.png", 0, 0

End Sub

I know it really needs cleaned up, but I need to make it work first.
 

Some videos you may like

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest

Watch MrExcel Video

Forum statistics

Threads
1,118,706
Messages
5,573,720
Members
412,548
Latest member
wallisonlac
Top