質問

私は投稿しました IE9は、ファイルをダウンロードするためのSendKeysを受け入れません, 、しかし、この問題は、別の質問を正当化するために私が受け取った答えとは十分に分離されています。私の問題は、IE9に次のいずれかを受け入れることができないことです SendKeys.私は試みました ページダウン, タブ, 、すべての 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_Initialize サブ...それが役立つかどうかはわかりませんが、なぜこれが機能しないのか、IEにキーを送信する方法に関するヘルプがあれば大歓迎です。

役に立ちましたか?

解決

これは実際にはのコピーです 私の答えこの質問,しかし、それはまだ適用されるかもしれません。

あなたがしようとすると、IEウィンドウがアクティブですか SendKeys?そうでない場合、これは動作しないことを説明します。

ウィンドウを有効にするには:

モジュールの先頭に、次のコード行を置きます:

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)、あなたはまだコード内のオブジェクトを使用して対話することができます。


今、私はあなたがアプリケーションを使用しているのを見ます。クリックした後は待ってくださいが、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
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top