Home > Mobile >  Unable to embed images right next to their concerning asins in a worksheet
Unable to embed images right next to their concerning asins in a worksheet

Time:09-18

I'm trying to create a script in vba in combination with IE to scrape image link from some webpages and embed the same right next to ASIN column in a worksheet. In column A there are list of ASINs' and in column B, I would like to embed the image like this. So, the script takes asin from column A and forms a qualified link by appeneding that asin to this base link https://www.amazon.in/dp/. This is the address of one such webpage.

Here are some asins:

B08SRFZX5Z
B08KKJQ8N7
B081RC61YN

Which forms the following links:

https://www.amazon.in/dp/B08SRFZX5Z
https://www.amazon.in/dp/B08KKJQ8N7
https://www.amazon.in/dp/B081RC61YN

I've tried with:

Sub GetImages()
    Const URL$ = "https://www.amazon.in/dp/"
    Dim IE As Object, ws As Worksheet, cel As Range
    
    Set ws = ThisWorkbook.Worksheets("Sheet1")
    Set IE = CreateObject("InternetExplorer.Application")

    For Each cel In ws.Range("A2:A" & ws.Cells(Rows.count, 1).End(xlUp).Row)
        IE.Visible = False
        IE.navigate URL & cel
        While IE.Busy Or IE.readyState < 4: DoEvents: Wend
        ws.Range(cel(1, 2).Address) = IE.document.querySelector("[id='imgTagWrapperId'] > img").getAttribute("src")
    Next cel
End Sub

How to embed image right next to asin list in a worksheet?

CodePudding user response:

Please, test the next code (Without IE):

Sub GetImagesNoIE()
 Dim imageUrl$, p As Shape, ws As Worksheet, cel As Range, rng As Range, arrSrc
 Dim Http As Object, HTMLDoc As Object    'late binding
 'in order to benefit of intellisense suggestions the next two references should be added:
 'Microsoft WinHTTP Services, version 5.1
 'Microsoft HTML Object Library
 Const URL$ = "https://www.amazon.in/dp/"
 
 Set Http = CreateObject("WinHttp.WinHttpRequest.5.1")
 Set HTMLDoc = CreateObject("htmlfile")
 
 Set ws = ActiveSheet 'ThisWorkbook.Worksheets("Sheet1")
 'delete the previous (existing) shapes in column B:B, if any:
 Application.ScreenUpdating = False
 Application.EnableEvents = False
  Set rng = ws.Range("B:B")
  For Each p In ws.Shapes
      If Not Intersect(rng, p.TopLeftCell) Then p.Delete
  Next

  For Each cel In ws.Range("A2:A" & ws.Cells(Rows.Count, 1).End(xlUp).Row)
    If cel.Value <> "" Then
        With Http
            .Open "GET", URL & cel.Value, False
            .setRequestHeader "User-Agent", "Firefox"
            .send
            HTMLDoc.body.innerHTML = .responseText
        End With
        If InStr(HTMLDoc.body.innerText, "We're sorry. The Web address you entered is not a functioning page on our site") = 0 Then
             imageUrl = HTMLDoc.querySelector("[id='imgTagWrapperId'] > img").getAttribute("src")
             Set p = ws.Shapes.AddPicture(imageUrl, msoFalse, msoTrue, cel.Offset(0, 1).Left, _
                                   cel.Offset(0, 1).Top, cel.Offset(0, 1).Width, cel.Offset(0, 1).Height)
            
        Else
            cel.Offset(0, 1).Value = "The Web address you entered is not a functioning page on our site"
        End If
    End If
  Next
  Application.ScreenUpdating = False
  Application.EnableEvents = False
  MsgBox "Ready..."
End Sub

CodePudding user response:

Use AddPicture method insert picture using URL:

ws.Shapes.AddPicture("image url", msoFalse, msoTrue, cel.OffSet(0,1).Left, cel.OffSet(0,1).Top, -1. -1)

-1 indicate use the original size but you can change the width and height at your desired size.

https://docs.microsoft.com/en-us/office/vba/api/excel.shapes.addpicture

  • Related