سؤال

نشرت لي على إي 9 عدم قبول سيندكيس لتحميل ملف, ، ولكن هذه المشكلة منفصلة بما فيه الكفاية عن الإجابة التي تلقيتها لتبرير سؤال آخر.مشكلتي هي أنني لا أستطيع الحصول على إي 9 لقبول أي من 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

أنا في خسارة كاملة لأنه يبدو أن معظم المنتديات أو الدروس لا تفعل أي شيء مختلف ل إي 9.يتم إنشاء كائن إي في وحدة فئة وتهيئتها في Class_Initialize الفرعية.أنا لست متأكدا إذا كان ذلك يساعد أي ، ولكن ليس لدي حقا أي فكرة لماذا هذا لا يعمل وأي مساعدة حول كيفية إرسال مفاتيح إلى إي سيكون موضع تقدير كبير.

هل كانت مفيدة؟

المحلول

هذا هو في الواقع نسخة من جوابي إلى هذا السؤال, ، لكنها قد لا تزال سارية.

هل نافذة إي نشطة عند محاولة الخاص بك SendKeys?إذا لم يكن الأمر كذلك ، فهذا يفسر أنه لا يعمل.

لتفعيل نافذتك:

في بداية الوحدة النمطية الخاصة بك ، ضع هذا السطر من التعليمات البرمجية:

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

سيسمح لك هذا بالوصول إلى SetForegroundWindow وظيفة مدمجة في ويندوز.

في التعليمات البرمجية الخاصة بك ، أثناء التفاعل مع كائن إي الخاص بك ، تسجيل هوند لتلك النافذة مثل ذلك:

Dim HWNDSrc As Long
HWNDSrc = ie.HWND

ثم بعد تحميل الصفحة ، استخدم هذا للمتابعة ، ثم أرسل الإجراءات الرئيسية الخاصة بك:

SetForegroundWindow HWNDSrc

ومع ذلك ، قد لا يكون هذا ضروريا ، اعتمادا على كيفية تفاعلك مع إي.بمعنى آخر ، إذا لم تكن بحاجة إلى رؤية / لمس النافذة (أنت تفعل من أجل SendKeys) ، لا يزال بإمكانك التفاعل باستخدام الكائن في التعليمات البرمجية.


الآن ، أراك تستخدم التطبيق.انتظر بعد النقر ، ولكن هذا لا يضمن تحميل صفحة إي.يجب أن تساعد هذه الوظيفة في ذلك.

 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