Results 1 to 2 of 2

Insert first image into found on a search engine based on a keyword

Threaded View

  1. #1
    Registered User
    Join Date
    06-05-2023
    Location
    london, england
    MS-Off Ver
    10
    Posts
    2

    Insert first image into found on a search engine based on a keyword

    Im trying to find a script that inserts the first image based on a keyword i.e. A1 "Abelia chinensis Variegata" finds a picture of the plant and puts it in B1

    i have found the script below taken from a previous thread https://www.excelforum.com/excel-pro...from-cell.html

    but i get

    Althought the keyword is column D the results in A are almost all zeros

    So unable to check if this does what i need it to.

    Any help would be greatly appreciated.

    Althought the keyword is column D the results in A are almost all zeros

    So unable to check if this does what i need it to.

    Any help would be greatly appreciated.

    'Requires additional references to Microsoft Internet Control
    'Requires additional HTML object library
    Public Sub imagedownload()
    Dim IE As InternetExplorer
    Dim HTMLdoc As HTMLDocument
    Dim imgElements As IHTMLElementCollection
    Dim imgElement As HTMLImg
    Dim aElement As HTMLAnchorElement
    Dim n As Integer, I As Integer
    Dim Url As String, url2 As String
    Dim m, LastRow As Long
    Dim furl As String
    
    Sheets("one").Select
    
    LastRow = Range("D" & Rows.Count).End(xlUp).Row
    
    For I = 3 To LastRow
    
    Url = "https://www.google.com/search?q=" & Cells(I, 4) & "&source=lnms&tbm=isch&sa=X&rnd=1"
    Set IE = New InternetExplorer
    On Error Resume Next
    
    Sheets("one").Select
    With IE
    .Visible = False
    .navigate Url 'sWebSiteURL
    
    Do Until .readyState = 4: DoEvents: Loop
    'Do Until IE.document.readyState = "complete": DoEvents: Loop
    
    Set HTMLdoc = .document
    
    Set imgElements = HTMLdoc.getElementsByTagName("IMG")
    
    n = 1
    For Each imgElement In imgElements
    On Error Resume Next
    
    
    If InStr(imgElement.src, sImageSearchString) Then
    If imgElement.ParentNode.nodeName = "A" Then
    Set aElement = imgElement.ParentNode
    'Cells(n, 2).Value = imgElement.src
    'Cells(n, 3).Value = aElement.href
    If n = 2 Then
    url2 = aElement.href 'imgElement.src
    url3 = imgElement.src 'aElement.href
    GoTo done:
    End If
    
    n = n + 1
    
    End If
    End If
    Next
    
    done:
    furl = InStrRev(url2, "&imgrefurl=", -1)
    
    furl = Mid(url2, 37, furl - 37)
    
    
    
    Sheets("two").Select
    
    'On Error Resume Next
    Cells(I, 1) = furl
    Set m = ActiveSheet.Pictures.Insert(furl)
    With Cells(I, 1)
    t = .Top
    l = .Left
    w = .Width
    h = .Height
    End With
    With m
    .Top = t
    .Left = l
    .ShapeRange.Width = w
    .ShapeRange.Height = h
    End With
    
    Sheets("one").Select
    
    IE.Quit
    Set IE = Nothing
    End With
    Next
    
    End Sub
    Last edited by AliGW; 06-27-2023 at 08:44 AM. Reason: URL fixed.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Insert the first image found with image search with criteria taken from cell.
    By lifegame1 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-12-2022, 10:02 AM
  2. Searching for multiple keywords in multiple sources of text
    By Bart.. in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 01-12-2022, 10:33 AM
  3. [SOLVED] Insert the first image found with image search (e.g google) with criteria taken from cell.
    By themob212 in forum Excel Programming / VBA / Macros
    Replies: 31
    Last Post: 04-14-2021, 12:29 PM
  4. Search cells C1:C3000 for keyword and if exists place a keyword in A1:A3000
    By GregQuick in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 11-24-2015, 07:38 PM
  5. Keyword search, and insert row.
    By seanobrathvilly in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 01-20-2014, 07:29 PM
  6. Prompting a keyword to search, then list all the lines where it is found
    By pylauzier in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-28-2011, 04:27 PM
  7. Populate a cell based on a keyword it found in another
    By ghobbit in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 05-15-2006, 11:47 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1