IE 9 不接受 SendKeys [英] IE 9 not accepting SendKeys

查看:14
本文介绍了IE 9 不接受 SendKeys的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发布在 IE 9 不接受 SendKeys下载一个文件,但这个问题与我收到的答案是分开的,足以证明另一个问题的合理性.我的问题是我无法让 IE 9 接受任何 SendKeys.我尝试了 Page DownTab、所有的 F# 键,但它们都不起作用.

I posted on IE 9 not accepting SendKeys to download a file, but this problem is separate enough from the answer I received to justify another question. My problem is that I can't get IE 9 to accept any of the SendKeys. I have attempted Page Down, Tab, all of the F# keys, and none of them work.

这是我使用的代码:

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 的帮助将不胜感激.

I'm at a total loss because it seems like most forums or tutorials don't do anything different for IE 9. The IE object is created in a class module and initialized in the Class_Initialize sub. I am not sure if that helps any, but I really have no idea why this isn't working and any help on how to send keys to IE would be greatly appreciated.

推荐答案

这实际上是我的回答的副本这个问题,但它可能仍然适用.

This is actually a copy of my answer to this question, but it may still apply.

当您尝试 SendKeys 时,IE 窗口是否处于活动状态?如果没有,这将解释它不起作用.

Is the IE window active when you try your SendKeys? If not, this would explain it not working.

激活您的窗口:

在你的模块的开头,输入这行代码:

At the beginning of your module, put this line of code:

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

这将允许您访问 Windows 内置的 SetForegroundWindow 函数.

This will allow you to access the SetForegroundWindow function built into Windows.

在您的代码中,在与 IE 对象交互时,记录该窗口的 HWND,如下所示:

In your code, while interacting with your IE object, record the HWND for that window like so:

Dim HWNDSrc As Long
HWNDSrc = ie.HWND

然后在您加载页面后,使用它继续,然后发送您的关键操作:

Then after you've loaded the page, use this to continue, then send your key actions:

SetForegroundWindow HWNDSrc

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

However, this may not be necessary, depending on how you are interacting with IE. In other words, if you don't need to see/touch the window (you do for SendKeys), you can still interact using the object in code.

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

Now, I see you using Application.Wait after you click, but that does not guarantee the IE page has loaded. This function should help with that.

 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

<小时>

冒着冗长的风险,我已经用这些建议更新了您的代码......


At the risk of being long-winded, I've updated your code with these suggestions...

' 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

这篇关于IE 9 不接受 SendKeys的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆