Results 1 to 10 of 31

Thread: Lookup First URL From Google Search Result Using VBA

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #5
    Administrator Excel Fox's Avatar
    Join Date
    Mar 2011
    Posts
    1,401
    Rep Power
    10
    You could have corrected it by increasing the Wait time, but I thought let's go one step better.

    Try this...

    Code:
    Option Explicit
    Dim var As Variant
    
    
    Sub SearchGoogle()
        
        Dim IE As InternetExplorer
        Dim objDoc As HTMLDocument
        Dim objTextBox As HTMLInputElement
        Dim objSubmitButton As HTMLButtonElement
        Dim rng As Range
        Dim obj As Object
        Dim lngCol As Long
        
        Worksheets("Sheet1").Range("B2:B" & Rows.Count).ClearContents
        Set rng = Worksheets("URLEncoding").Cells(1).CurrentRegion
        ReDim var(1 To rng.Rows.Count, 1 To 2)
        For Each rng In rng.Columns(1).Cells
            var(rng.Row, 1) = rng.Text
            var(rng.Row, 2) = rng.Offset(, 1).Text
        Next rng
        Const strURL = "http://www.google.com/#output=search&sclient=psy-ab&q=|+|&rlz=1W1LENP_enIN497&oq=|+|&gs_l=hp.12..0l4.22785.22785.0.52909.1.1.0.0.0.0.183.183.0j1.1.0...0.0...1c..12.psy-ab.Zd3_T9FwkMg&pbx=1&bav=on.2,or.r_qf.&bvm=bv.46471029,d.bmk&fp=dd21897ce84632f&biw=1704&bih=928"
        Set IE = New InternetExplorer 'CreateObject("InternetExplorer.Application")
        IE.Visible = True
        For Each rng In Worksheets("Sheet1").Range("A2:A20000")
            INavigate IE, "http://www.google.com/"
            If Not IsEmpty(rng) Then
                INavigate IE, Replace(strURL, "|+|", URLENCODED(rng.Value))
                While obj Is Nothing
                    Set obj = Nothing
                    On Error Resume Next
                    Set obj = IE.document.getElementById("ires")
                    Err.Clear: On Error GoTo 0: On Error GoTo -1
                Wend
                While obj.readyState <> "complete": Wend
                Set obj = Nothing
                Set objDoc = IE.document
                For Each obj In objDoc.getElementsByTagName("A")
                    If MIMEINCLUDE(obj.mimeType) Then lngCol = lngCol + 1
                    If lngCol = 6 Then
                        rng.Offset(, 1).Value = obj.href
                        lngCol = 0
                        Exit For
                    End If
                Next obj
            Else
                Exit For
            End If
            Set obj = Nothing
        Next rng
        Set objDoc = Nothing
        Set objTextBox = Nothing
        Set rng = Nothing
        IE.Quit
    
    
    End Sub
    
    
    Private Function URLENCODED(str) As String
    
    
        Dim lng As Long
        Dim lngRow As Long
        For lngRow = LBound(var) To UBound(var)
            str = Replace(str, var(lngRow, 1), var(lngRow, 2))
        Next lngRow
        URLENCODED = str
        
    End Function
    
    
    Private Function MIMEINCLUDE(str As String)
    
    
        Const strDOM As String = "COM,ORG,IN,AU,US"
        Dim lng As Long
        For lng = LBound(Split(strDOM, ",")) To UBound(Split(strDOM, ","))
            If str = Split(strDOM, ",")(lng) & "/ File" Then
                MIMEINCLUDE = True: Exit For
            End If
        Next lng
        
    End Function
    
    
    Private Sub INavigate(IE As InternetExplorer, strURL)
    
    
        IE.Navigate strURL
        While IE.Busy: Wend 'wait until IE is done loading page.
        While IE.readyState <> READYSTATE_COMPLETE: Wend
            
    End Sub
    Attached Files Attached Files
    A dream is not something you see when you are asleep, but something you strive for when you are awake.

    It's usually a bad idea to say that something can't be done.

    The difference between dream and aim, is that one requires soundless sleep to see and the other requires sleepless efforts to achieve

    Join us at Facebook

Similar Threads

  1. Replies: 4
    Last Post: 04-24-2013, 10:04 AM
  2. Find the highest then lookup result
    By Stalker in forum Excel Help
    Replies: 4
    Last Post: 04-02-2013, 02:04 PM
  3. Import html source of url list in each cell
    By Sergio Alfaro Lloret in forum Excel Help
    Replies: 8
    Last Post: 07-31-2012, 03:03 AM
  4. VBA Function to Search in Array
    By Admin in forum Excel and VBA Tips and Tricks
    Replies: 0
    Last Post: 04-10-2012, 11:34 AM
  5. VBA - Excel: Disable Internet / Google
    By technicalupload in forum Excel Help
    Replies: 3
    Last Post: 10-06-2011, 09:18 AM

Tags for this Thread

Posting Permissions

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