IE 9不接受的SendKeys [英] IE 9 not accepting SendKeys
问题描述
我贴在<一个href=\"http://stackoverflow.com/questions/11638933/excel-vba-sendkeys-not-causing-ie-9-to-save-download\">IE 9不接受的SendKeys下载一个文件,但这个问题是我收到证明另一个问题的答案不够独立。我的问题是我不能让IE 9可以接受任何的的SendKeys
。我试图<大骨节病> Page Down键骨节病>,<大骨节病>标签骨节病>,所有的<大骨节病> 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.
下面是code我使用的:
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
我在一个总损失,因为它似乎最喜欢的论坛或教程,不要做什么不同的IE 9类模块创建的对象IE并初始化在 Class_Initialize
子。我不知道有没有什么帮助的,但我真的不知道为什么这不工作,并就如何键发送到IE浏览器将是极大的AP preciated任何帮助。
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.
当您尝试是IE窗口活跃你的的SendKeys
?如果没有,这可以解释它没有工作。
Is the IE window active when you try your SendKeys
? If not, this would explain it not working.
要激活窗口:
在你的模块开始,就把这条线code的:
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.
在您的code,同时与你的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
),你仍然可以交互使用对象code
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
在被啰嗦的风险,我已经与这些建议更新了code ...
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屋!