vba IE 9 不接受 SendKeys
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11655591/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
IE 9 not accepting SendKeys
提问by derigible
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.
我在IE 9上发布了不接受 SendKeys 来下载文件的帖子,但是这个问题与我收到的答案是分开的,足以证明另一个问题的合理性。我的问题是我无法让 IE 9 接受任何SendKeys
. 我已经尝试了Page Down, Tab, 所有的F#键,但没有一个起作用。
Here is the code I am using:
这是我正在使用的代码:
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
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.
我完全不知所措,因为似乎大多数论坛或教程对 IE 9 没有任何不同。IE 对象是在类模块中创建的,并在Class_Initialize
子模块中初始化。我不确定这是否有帮助,但我真的不知道为什么这不起作用,任何有关如何将密钥发送到 IE 的帮助将不胜感激。
回答by Gaffi
This is actually a copy of my answerto this question, but it may still apply.
Is the IE window active when you try your SendKeys
? If not, this would explain it not working.
当您尝试使用 IE 时,IE 窗口是否处于活动状态SendKeys
?如果没有,这将解释它不起作用。
To activate your window:
要激活您的窗口:
At the beginning of your module, put this line of code:
在模块的开头,放置以下代码行:
Public Declare Function SetForegroundWindow Lib "user32" (ByVal HWND As Long) As Long
This will allow you to access the SetForegroundWindow
function built into Windows.
这将允许您访问SetForegroundWindow
Windows 内置的功能。
In your code, while interacting with your IE object, record the HWND for that window like so:
在您的代码中,在与 IE 对象交互时,记录该窗口的 HWND,如下所示:
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
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.
但是,这可能不是必需的,这取决于您与 IE 的交互方式。换句话说,如果您不需要查看/触摸窗口(您需要查看/触摸窗口SendKeys
),您仍然可以使用代码中的对象进行交互。
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.
现在,我看到您在单击后使用 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
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