+ Reply to Thread
Results 1 to 19 of 19

Return first image link from google search in excel using vba

Hybrid View

  1. #1
    Registered User
    Join Date
    10-14-2015
    Location
    macedonia
    MS-Off Ver
    2013
    Posts
    2

    Question Return first image link from google search in excel using vba

    It is possible to modify this code to search for large images and to return the link of the first image. I tryed to modify the google link with the google image link but it doesent work
    Sub XMLHTTP()
    
    Dim url As String, lastRow As Long
    Dim XMLHTTP As Object, html As Object, objResultDiv As Object, objH3 As Object, link As Object
    Dim start_time As Date
    Dim end_time As Date
    
    lastRow = Range("A" & Rows.Count).End(xlUp).Row
    
    Dim cookie As String
    Dim result_cookie As String
    
    start_time = Time
    Debug.Print "start_time:" & start_time
    
    For i = 2 To lastRow
    
        url = "https://www.google.com/search?q=" & Cells(i, 1) & "&rnd=" & WorksheetFunction.RandBetween(1, 10000)
    
        Set XMLHTTP = CreateObject("MSXML2.serverXMLHTTP")
        XMLHTTP.Open "GET", url, False
        XMLHTTP.setRequestHeader "Content-Type", "text/xml"
        XMLHTTP.setRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:25.0) Gecko/20100101 Firefox/25.0"
        XMLHTTP.send
    
            Set html = CreateObject("htmlfile")
        html.body.innerHTML = XMLHTTP.ResponseText
        Set objResultDiv = html.getelementbyid("rso")
        Set objH3 = objResultDiv.getelementsbytagname("H3")(0)
        Set link = objH3.getelementsbytagname("a")(0)
    
    
        str_text = Replace(link.innerHTML, "<EM>", "")
        str_text = Replace(str_text, "</EM>", "")
    
        Cells(i, 2) = str_text
        Cells(i, 3) = link.href
        DoEvents
    Next
    
    end_time = Time
    Debug.Print "end_time:" & end_time
    
    Debug.Print "done" & "Time taken : " & DateDiff("n", start_time, end_time)
    MsgBox "done" & "Time taken : " & DateDiff("n", start_time, end_time)
    End Sub

  2. #2
    Forum Contributor
    Join Date
    06-24-2005
    Location
    excelliot.com
    MS-Off Ver
    2007
    Posts
    325

    Re: Return first image link from google search in excel using vba

    hi

    check this code

    Public Sub Test()
    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 = Range("A" & Rows.Count).End(xlUp).Row
    
    For i = 1 To lastRow
        url = "https://www.google.co.in/search?q=" & Cells(i, 1) & "&source=lnms&tbm=isch&sa=X&rnd=1"
        Set IE = New InternetExplorer
        
        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
            If InStr(imgElement.src, sImageSearchString) Then
                If imgElement.ParentNode.nodeName = "A" Then
                    Set aElement = imgElement.ParentNode
                    
                    url2 = imgElement.src
                    n = n + 1
                End If
            End If
        Next
        
        Cells(i, 2) = url2
        
    IE.Quit
    Set IE = Nothing
        End With
    Next
    
    End Sub
    Cheers!!

  3. #3
    Forum Contributor
    Join Date
    06-24-2005
    Location
    excelliot.com
    MS-Off Ver
    2007
    Posts
    325

    Re: Return first image link from google search in excel using vba

    Hi,

    My earlier code was not fetching first image, use this to fetch..

    Requirements In Column A write name of object for which you want image search, In B column you will get URL of source image & in C column you will get Image matching size of your cell, you can re-size your C column to make your image bigger or smaller before running macro..

    'Requires additional references to Microsoft Internet Control
    'Requires additional HTML object library
    
    Public Sub Fetch_Image()
    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
        
    lastRow = Range("A" & Rows.Count).End(xlUp).Row
    
    For i = 1 To lastRow
        url = "https://www.google.co.in/search?q=" & Cells(i, 1) & "&source=lnms&tbm=isch&sa=X&rnd=1"
        Set IE = New InternetExplorer
        
        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
            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, 40, furl - 40)
        Cells(i, 2) = furl
        Set m = ActiveSheet.Pictures.Insert(furl)
        With Cells(i, 3)
        t = .Top
        l = .Left
        w = .Width
        h = .Height
        End With
        With m
        .Top = t
        .Left = l
        .ShapeRange.Width = w
        .ShapeRange.Height = h
        End With
        
    IE.Quit
    Set IE = Nothing
        End With
    Next
    
    End Sub
    Cheers!!

  4. #4
    Registered User
    Join Date
    05-11-2016
    Location
    OTA
    MS-Off Ver
    2010
    Posts
    10

    Re: Return first image link from google search in excel using vba

    Hi everyone, thank you very much for your help! the code is great but I get an error

    I think the error is due to the format %33%A... of the fetched link: http%3A%2F%2Fweknowyourdreamz.com%2Fimages%2Fbaby%2Fbaby-02.jpg

    can you help please?

  5. #5
    Registered User
    Join Date
    07-20-2016
    Location
    USA
    MS-Off Ver
    2016
    Posts
    2

    Re: Return first image link from google search in excel using vba

    Quote Originally Posted by Vincent121 View Post
    Hi everyone, thank you very much for your help! the code is great but I get an error

    I think the error is due to the format %33%A... of the fetched link: http%3A%2F%2Fweknowyourdreamz.com%2Fimages%2Fbaby%2Fbaby-02.jpg

    can you help please?
    i get this same error. even after making sure all references are set.

  6. #6
    Forum Contributor
    Join Date
    06-24-2005
    Location
    excelliot.com
    MS-Off Ver
    2007
    Posts
    325

    Re: Return first image link from google search in excel using vba

    Quote Originally Posted by jyeldell View Post
    i get this same error. even after making sure all references are set.
    can you share exact what error you are getting..

  7. #7
    Registered User
    Join Date
    07-20-2016
    Location
    USA
    MS-Off Ver
    2016
    Posts
    2

    Re: Return first image link from google search in excel using vba

    Hello All (and thanks ExcelElliot).

    Using ExcelElliot's code as is, I received a compile error (even after making sure that all references were set).

    It would error out on the line:

    Set m = ActiveSheet.Pictures.Insert(furl)
    After alot of digging, i discovered that the url passed to the variable "furl" was not getting fully translated into a usable url.....meaning that it contained many instances of "%2F" instead of a forward slash.

    I was able to get his code running by adding the following lines of code that further translate the resultant image URL to remove any instances of %2F:

    furl = Replace(furl, "%2F", "/")

  8. #8
    Registered User
    Join Date
    08-03-2016
    Location
    korea
    MS-Off Ver
    2007
    Posts
    1

    Re: Return first image link from google search in excel using vba

    i got error with this "can't get an insert method of picture class"
    so,image doesn't come .
    what do I do for this error?
    Last edited by estone5; 08-03-2016 at 04:07 AM.

  9. #9
    Registered User
    Join Date
    06-06-2016
    Location
    USA
    MS-Off Ver
    13
    Posts
    6

    Re: Return first image link from google search in excel using vba

    Hello

    When I run excilliot's code it says Compile Error: User-defined type not defined. And it is referring to the "Dim IE as InternetExplorer"

  10. #10
    Forum Contributor
    Join Date
    06-24-2005
    Location
    excelliot.com
    MS-Off Ver
    2007
    Posts
    325

    Re: Return first image link from google search in excel using vba

    Quote Originally Posted by sawvba View Post
    Hello

    When I run excilliot's code it says Compile Error: User-defined type not defined. And it is referring to the "Dim IE as InternetExplorer"
    Hi
    ,
    You have to set a reference to the IE library for the code to work..

    Cheers!!

  11. #11
    Forum Contributor
    Join Date
    06-24-2005
    Location
    excelliot.com
    MS-Off Ver
    2007
    Posts
    325

    Re: Return first image link from google search in excel using vba

    Alternatively you can remove that line and add following one if you are not sure about howto set references:

    Dim IE as Object
    Set IE = CreateObject("InternetExplorerer.Application")
    Cheers!!

  12. #12
    Registered User
    Join Date
    10-21-2013
    Location
    london
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: Return first image link from google search in excel using vba

    Hi, could anybody assist, i am adding Exelliots code above and i get runtime error 5 , invalid procedure call when i run the code?

    when i add values to Column A nothing is happening

    Any help would be great! Thanks

  13. #13
    Registered User
    Join Date
    03-04-2021
    Location
    Dubai, UAE
    MS-Off Ver
    OFFICE 365
    Posts
    2

    Re: Return first image link from google search in excel using vba

    im getting the same error.
    Any help would be great!

  14. #14
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    53,052

    Re: Return first image link from google search in excel using vba

    Quote Originally Posted by cjain_560 View Post
    im getting the same error.
    Any help would be great!
    Did you really not see the post IMMEDIATELY above yours (the very past post you would see as you type your post???
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools
    3. Click on the star if you think someone helped you

    Regards
    Ford

  15. #15
    Registered User
    Join Date
    03-04-2021
    Location
    Dubai, UAE
    MS-Off Ver
    OFFICE 365
    Posts
    2
    Quote Originally Posted by FDibbins View Post
    Did you really not see the post IMMEDIATELY above yours (the very past post you would see as you type your post???
    Do you really see me asking a question there ? Don’t be a smart one, if you can help, so be it, otherwise let others reply.

  16. #16
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    53,052

    Re: Return first image link from google search in excel using vba

    thegr812k welcome to the forum

    Unfortunately your post does not comply with Rule 2 of our Forum RULES. Do not post a question in the thread of another member -- start your own thread.

    If you feel an existing thread is particularly relevant to your need, provide a link to the other thread in your new thread.

    Old threads are often only monitored by the original participants. New threads not only open you up to all possible participants again, they typically get faster response, too.

  17. #17
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    53,052

    Re: Return first image link from google search in excel using vba

    wind your neck in, newbie! You posted a question phrased as a statement. If you want an answer, open your own thread - and take a few minutes to read our rules.

  18. #18
    Registered User
    Join Date
    09-20-2022
    Location
    new york
    MS-Off Ver
    365
    Posts
    2

    Re: Return first image link from google search in excel using vba

    little lost getting a not declared error, anyone have a working workbook to follow?

  19. #19
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    53,052

    Re: Return first image link from google search in excel using vba

    Quote Originally Posted by pborreronyc View Post
    little lost getting a not declared error, anyone have a working workbook to follow?
    Administrative Note:

    Welcome to the forum.

    We are happy to help, however whilst you feel your request is similar to this thread, experience has shown that things soon get confusing when answers refer to particular cells/ranges/sheets which are unique to your post and not relevant to the original.

    Please see Forum Rule #4 about hijacking and start a new thread for your query.

    If you are not familiar with how to start a new thread see the FAQ: How to start a new thread

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Autoimport Google Image Search Result into Excel
    By awesummusik in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-14-2021, 12:29 PM
  2. Google image search from cell contents of excel file
    By merQrey in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 07-24-2020, 01:01 AM
  3. Replies: 0
    Last Post: 07-08-2014, 06:27 AM
  4. Macro NEED Remake- search in google and copy link
    By kunmateo93 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-06-2014, 12:19 PM
  5. [SOLVED] Google search link of text in Column A help?
    By brianroe88 in forum Excel General
    Replies: 13
    Last Post: 09-24-2013, 01:12 PM
  6. Replies: 0
    Last Post: 11-05-2009, 04:47 AM

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