1

我发布在IE 9 not accepting SendKeys to download a file,但这个问题与我收到的答案分开,以证明另一个问题。我的问题是,我无法让IE 9接受任何SendKeys。我试过Page DownTab,所有的F#键,并且它们都不起作用。IE 9不接受SendKeys

这里是我使用的代码:

Dim ie As Object 

'This creates the IE object 
Sub initializeIE() 
    'call this subprocedure to start internet explorer up 
    Set ie = CreateObject("internetexplorer.application") 
    pos = 1 
End Sub 

'Initialize the class object 
Private Sub Class_Initialize() 
    initializeIE 
End Sub 

Function followLinkByText(thetext As String) As Boolean 
    'clicks the first link that has the specified text 
    Dim alink As Variant 

    'Loops through every anchor in html document until specified text is found 
    ' then clicks the link 
    For Each alink In ie.document.Links 
    If alink.innerHTML = thetext Then 
      alink.Click 
      'waitForLoad 
      Application.Wait Now + TimeValue("00:00:01") 
      Application.SendKeys "{PGDN}", True 
      Application.SendKeys "{PGUP}", True 
      'I've also tried calling it without Application before it 
      SendKeys "{F1}", True 
      SendKeys "{F2}", True 
      'Etc... Each of these not being received by IE 9 

      followLinkByText = True 
      Exit Function 
     End If 
    Next 

End Function 

我在一个总的损失,因为它看起来像大多数论坛或教程没有做任何的IE 9的IE对象中创建不同一个班级模块,并在Class_Initialize子中初始化。我不知道这是否有帮助,但我真的不知道为什么这不起作用,任何关于如何发送密钥到IE的帮助将不胜感激。

+0

你有多个选项卡打开?我没有具体自动化IE9的经验,但我听说(无法找到参考ATM),这有所作为。 – Gaffi 2012-07-25 17:55:52

+0

不,宏只打开IE的一个实例,只打开一个选项卡。它到目前为止只导航到一个URL。 – derigible 2012-07-25 18:11:52

+0

这可能是你的实际问题的一个转移,但你想用'SendKeys'完成什么?大多数操作可以在没有*的情况下完成。 – Gaffi 2012-07-25 18:16:05

回答

3

这实际上是my answerthis question的副本,但它可能仍然适用。

当您尝试使用SendKeys时,IE窗口是否处于活动状态?如果没有,这将解释它无法正常工作。

要激活你的窗口:

在你的模块开始,就把这行代码:

Public Declare Function SetForegroundWindow Lib "user32" (ByVal HWND As Long) As Long 

这将允许您访问Windows中内置的SetForegroundWindow功能。

在你的代码,而与你的IE对象交互,记录HWND为窗口,像这样:

Dim HWNDSrc As Long 
HWNDSrc = ie.HWND 

然后加载了页面之后,可以使用,这样下去,然后把你的关键行动:

SetForegroundWindow HWNDSrc 

但是,这可能不是必需的,这取决于您如何与IE进行交互。换句话说,如果你不需要看/触摸窗口(你为SendKeys),你仍然可以使用代码中的对象进行交互。


现在,我看到您使用Application.Wait单击后,但不能保证IE页面已加载。这个功能应该对此有所帮助。

Public Sub WaitForIE(myIEwindow As InternetExplorer, HWND As Long, WaitTime As Integer) 

    ' Add pauses/waits so that window action can actually 
    ' begin AND finish before trying to read from myIEWindow. 

    ' myIEWindow is the IE object currently in use 
    ' HWND is the HWND for myIEWindow 
    ' The above two variables are both used for redundancy/failsafe purposes. 
    ' WaitTime is the amount of time (in seconds) to wait at each step below. 
    ' This is variablized because some pages are known to take longer than 
    ' others to load, and some pages with frames may be partially loaded, 
    ' which can incorrectly return an READYSTATE_COMPLETE status, etc. 

    Dim OpenIETitle As SHDocVw.InternetExplorer 

    Application.Wait DateAdd("s", WaitTime, Now()) 

    Do Until myIEwindow.ReadyState = READYSTATE_COMPLETE 
     ' Wait until IE is done loading page and/or user actions are done. 
    Loop 

    Application.Wait DateAdd("s", WaitTime, Now()) 

    While myIEwindow.Busy 
     DoEvents ' Wait until IE is done loading page and/or user actions are done. 
    Wend 

    On Error Resume Next 
    ' Make sure our window still exists and was not closed for some reason... 
    For Each OpenIETitle In objShellWindows 
     If OpenIETitle.HWND = HWND Then 
      If Err.Number = 0 Then 
       Set myIEwindow = OpenIETitle 
       Exit For 
      Else 
       Err.Clear 
      End If 
     End If 
    Next OpenIETitle 
    On Error GoTo 0 

End Sub 

在被啰嗦的风险,我已经与这些建议更新你的代码...

' Added by Gaffi 
Public Declare Function SetForegroundWindow Lib "user32" (ByVal HWND As Long) As Long 
Dim HWNDSrc As Long 

Dim ie As Object 


'This creates the IE object 
Sub initializeIE() 
    'call this subprocedure to start internet explorer up 
    Set ie = CreateObject("internetexplorer.application") 

' Added by Gaffi 
    HWNDSrc = ie.HWND 

    pos = 1 
End Sub 

'Initialize the class object 
Private Sub Class_Initialize() 
    initializeIE 
End Sub 

Function followLinkByText(thetext As String) As Boolean 
    'clicks the first link that has the specified text 
    Dim alink As Variant 

    'Loops through every anchor in html document until specified text is found 
    ' then clicks the link 
    For Each alink In ie.document.Links 
    If alink.innerHTML = thetext Then 
      alink.Click 
      'waitForLoad 

' Added by Gaffi 
      WaitForIE ie, HWNDSrc, 1 
      SetForegroundWindow HWNDSrc 

      'Application.Wait Now + TimeValue("00:00:01") 
      Application.SendKeys "{PGDN}", True 
      Application.SendKeys "{PGUP}", True 
      'I've also tried calling it without Application before it 
      SendKeys "{F1}", True 
      SendKeys "{F2}", True 
      'Etc... Each of these not being received by IE 9 

      followLinkByText = True 
      Exit Function 
     End If 
    Next 

End Function 
+0

这正是问题所在。我认为这可能是一个焦点问题,但我对IE自动化不太了解,无法知道如何查看窗口是否正在使用。谢谢您的帮助! – derigible 2012-07-25 20:26:32

+0

没问题。很高兴我能帮上忙! – Gaffi 2012-07-25 20:27:04