Episode #125 of the Stack Overflow podcast is here. We talk Tilde Club and mechanical keyboards. Listen now
22 of 31 label loop

Excel VBA +, 465 463 461 437 427 419 418 415 407 395 388 382 369 367 356 348 346 Bytes

Must be run in a clean module. No longer necessary.

NOTE: Uses References to

  • Microsoft HTML Object Library
  • Microsoft Internet Controls
  • Microsoft Scripting Runtime


Full Sub routine that takes input n (short for name) of expected type Variant/String and outputs the first google image search result for the query of that name and glamour shot to the `Sheets(1) object

Sub g(n)
Set i=New InternetExplorer
l="google.nl/search?tbm=isch&q="&Replace(n," ","+")&"+glamour+shot"
For y=0To 1
i.navigate l
If i.readyState<4 Then DoEvents:Goto M
Do Until InStr(1,l,IIf(y,"yp","res"))
Set x=i.document.getElementsByTagName(IIf(y,"img","a"))(j)
If y Then l=x.src Else l=x.href
Sheets(1).Pictures.Insert l
End Sub

-2 bytes for removing white space in If InStr(1, a.href, "imgres") Then

-2 bytes for changing .navigate (a.href) to .navigate a.href

-27 bytes for reducing for each a in ... if (...) then ... end if .. next loop to do until ... loop

-10 bytes for condensing imgres to res and encrypted to yp

-8 bytes for removing initalizers for j, k and assuming clean module

-1 byte for changing google.com to google.nl

-3 bytes for replacing Dim i as New ... with Set i=New ...

-8 bytes for removing SHDocVw. class reference

-12 bytes for removing with i block

-7 bytes for removing i.quit - this causes some memory leakage by leaving internet explorer open in the background, as such it is recommended that either the full ungolfed version is used instead or that the internet explorer task is terminated through task manager after use

-6 bytes for moving i.navigate into helper subroutine h

-13 bytes for moving Do Until ... Loop into helper subroutine

-2 bytes by moving j=0 into helper and removing ,j, ,0 (x2)

-11 bytes for converting to an anonymous immediate window function

-8 bytes for reducing helper function calls into for .. next loop

-2 bytes for reducing while .. wend loop to label: ... goto label loop

Ungolfed, 1304 Bytes

Option Private Module
Option Compare Text
Option Explicit
Option Base 0

Sub GlamourShot(ByVal name As String)

    Dim ie As New SHDocVw.InternetExplorer, _
        doc As MSHTML.HTMLDocument, _
        link As String, _
        j As Integer, _
        k As Integer

    With ie
        On Error GoTo CloseIE #'prevents memory leak

        Let .visible = True
        Call .navigate("www.google.com/search?tbm=isch&q=" & _
                    Replace(name, " ", "+") & _
        While .readyState <> READYSTATE_COMPLETE Or .Busy

        Set doc = .document

        Let j = 1
        Do Until InStr(1, link, "imgres") > 0
            Let link = doc.getElementsByTagName("a")(j).href
            Let j = j + 1

        Call .navigate(link)
        While .readyState <> READYSTATE_COMPLETE Or .Busy

        Let k = 1
        Do Until InStr(1, link, "encrypted") > 0
            Let link = doc.getElementsByTagName("img")(k).src
            Let k = k + 1

        With ThisWorkbook.ActiveSheet
            Call .Range("A1").Select
            Call .Pictures.Insert(link)
            Call .Activate
        End With
        Call .Quit
    End With
End Sub

Very poor quality gif

Usage Gif