gpt4 book ai didi

vba - IE 9 不接受 SendKeys

转载 作者:行者123 更新时间:2023-12-04 05:38:09 25 4
gpt4 key购买 nike

我发布在 IE 9 not accepting SendKeys to download a file ,但是这个问题与我收到的答案足以证明另一个问题的合理性。我的问题是我无法让 IE 9 接受任何 SendKeys .我尝试了 Page Down、Tab 和所有 F# 键,但它们都不起作用。

这是我正在使用的代码:

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 中初始化的。子。我不确定这是否有帮助,但我真的不知道为什么这不起作用,以及如何将 key 发送到 IE 的任何帮助将不胜感激。

最佳答案

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

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

要激活您的窗口:

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

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

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

在您的代码中,与您的 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

关于vba - IE 9 不接受 SendKeys,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11655591/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com