使用Visual Basic一次输入Google搜索一封信

时间:2012-05-29 14:16:04

标签: vba internet-explorer-9

我想使用VBA来运行Google搜索。对我来说,能够一次将一个字母输入搜索词到Google搜索栏中非常重要。这是我目前的代码:

myie.document.all("q").Value = "cheese"

这将在一个完整的时间间隔内将搜索词“cheese”加载到搜索栏中。我希望以更自然的方式进行搜索;将“c”键入搜索栏,然后输入“h”,然后输入“e”等...

感谢任何帮助

1 个答案:

答案 0 :(得分:0)

这是你在尝试的吗?

Option Explicit

Private Sub CommandButton1_Click()
    WebBrowser1.Navigate "Google.com"
End Sub

Private Sub CommandButton2_Click()
    Dim strtext As String
    Dim i As Long

    strtext = "cheese"

    For i = 1 To Len(strtext)
        WebBrowser1.Document.all("q").Value = WebBrowser1.Document.all("q").Value & Mid(strtext, i, 1)
        Wait 1
    Next i
End Sub

Private Sub Wait(ByVal nSec As Long)
    nSec = nSec + Timer
    While nSec > Timer
        DoEvents
    Wend
End Sub

如果您在键入时显示所有结果,那么您也可以使用此功能

Sub Sample()
    Dim IE As Object

    Set IE = CreateObject("InternetExplorer.Application")

    IE.Visible = True

    '~~> Choose one of the below
    IE.Navigate ("http://www.google.com/#hl=en&q=" & "Cheese") '<~~ US
    IE.Navigate ("http://www.google.co.uk/#hl=en&q=" & "Cheese") '<~~ UK
    IE.Navigate ("http://www.google.co.in/#hl=en&q=" & "Cheese") '<~~ INDIA
End Sub