문제

SendKeys를 수락하지 않음파일을 다운로드하십시오 그러나이 문제는 다른 질문을 정당화하기 위해받은 대답과 충분히 분리됩니다.내 문제는 내가 IE 9를 얻을 수 없다는 것입니다. SendKeys 중 하나를 수락 할 수 없습니다.나는 , , , , , 를 시도하고 그 중 누구도 작동하지 않습니다.

여기에있는 코드가 있습니다 :

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 모듈에서 생성되고 Class_Initialize SUB에서 초기화됩니다.나는 그것이 어떤 것도 도움이되는지 확실하지 않지만, 이것이 작동하지 않는 이유와 IE에 열쇠를 보내는 방법에 대한 도움이되는지는 정말로 알 수 없습니다.

도움이 되었습니까?

해결책

이것은 실제로 내 답변 ~ 이 질문 그러나 여전히 적용될 수 있습니다.

은 IE 창을 사용하여 SendKeys를 시도 할 때 활성화됩니까? 그렇지 않은 경우, 이것은 작동하지 않는 것을 설명합니다.

창을 활성화하려면 :

모듈의 시작 부분 에서이 코드 행을 넣으십시오 :

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

Windows에 내장 된 SetForegroundWindow 함수에 액세스 할 수 있습니다.

코드에서 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
.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top