Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba IE 9不接受发送密钥_Vba_Excel_Automation_Office Automation_Browser Automation - Fatal编程技术网

Vba IE 9不接受发送密钥

Vba IE 9不接受发送密钥,vba,excel,automation,office-automation,browser-automation,Vba,Excel,Automation,Office Automation,Browser Automation,我发了帖子,但这个问题与我收到的答案有足够的区别,足以证明另一个问题是正确的。我的问题是,我无法让IE 9接受任何发送键。我尝试过向下翻页,Tab键,所有的F#键,但都不起作用 以下是我正在使用的代码: Dim ie As Object 'This creates the IE object Sub initializeIE() 'call this subprocedure to start internet explorer up Set ie = CreateObject("

我发了帖子,但这个问题与我收到的答案有足够的区别,足以证明另一个问题是正确的。我的问题是,我无法让IE 9接受任何
发送键
。我尝试过向下翻页,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

我完全不知所措,因为似乎大多数论坛或教程对IE9没有任何不同。IE对象是在类模块中创建的,并在
class\u Initialize
子模块中初始化。我不确定这是否有帮助,但我真的不知道为什么这样做不起作用,如果您能帮助IE发送密钥,我们将不胜感激。

这实际上是to的副本,但它仍然适用

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

要激活窗口,请执行以下操作:

在模块开头,输入以下代码行:

Public Declare Function SetForegroundWindow Lib "user32" (ByVal HWND As Long) As Long
这将允许您访问Windows内置的
setForeGroundIndow
功能

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

Dim HWNDSrc As Long
HWNDSrc = ie.HWND
加载页面后,使用此选项继续,然后发送关键操作:

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


现在,我看到您正在使用应用程序。单击后等待,但这并不保证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

您是否打开了多个选项卡?我没有专门自动化IE9的经验,但我听说(找不到参考ATM)这会有所不同。不,宏只打开一个IE实例和一个选项卡。到目前为止,它只导航到一个URL。这可能是对您实际问题的转移,但使用
SendKeys
,您想实现什么?大多数操作都可以不用它来完成。我之前的帖子概述了我创建宏的尝试,但IE不接受我的sendkeys。Rout能够提供解决方案,但在他提供解决方案之前,我注意到sendkeys根本不起作用。如果我能想出如何发送密钥,这将有助于我将来尝试做类似的事情。明白了。我发现其他响应更为复杂,
SendKeys
可能是更好的选择。:-)我确实在这里发布了一个仍然适用的可能解决方案。这正是问题所在。我认为这可能是一个焦点问题,但我对IE自动化还不够熟悉,不知道如何确定窗口是否被使用。谢谢你的帮助!没问题。很高兴我能帮忙!