Excel SendKeys与Internet Explorer的交互

时间:2017-08-07 16:15:26

标签: excel vba excel-vba internet-explorer

以下Excel VBA代码已成功将所需文本插入Google网页。但是,如果删除“Set Wss”行并将“Wss.SendKeys”命令替换为“Application.Senkeys”,则代码无法插入文本。为什么是这样? “CreateObject(”WScript.Shell“)”是什么导致成功将文本插入到网页上?

Sub Macro2()
Set Wss = CreateObject("WScript.Shell")

Set ie = CreateObject("InternetExplorer.Application")

With ie
    .Visible = True
    .Navigate "https://www.google.com/"
    .Top = 0
    .Left = 0
    .Height = 1025
    .Width = 1925

' Loop until the page is fully loaded
        Do Until Not ie.Busy And ie.ReadyState = 4
            DoEvents
        Loop
        Application.Wait (Now + TimeValue("0:00:05"))
    End With

' Use SendKeys to insert text
    Wss.SendKeys "this"
    Application.Wait (Now + TimeValue("0:00:01"))
    Wss.SendKeys " is a"
    Application.Wait (Now + TimeValue("0:00:01"))
    Wss.SendKeys " test"
    Application.Wait (Now + TimeValue("0:00:01"))
End Sub

0 个答案:

没有答案