2017-08-03 330 views
1

我正在處理一個web表單文本框,當按下「Enter」鍵時,它將啓動對其內容的搜索。我知道如何啓動所有其他事件偵聽器,但我無法讓按下「Enter」事件觸發。它沒有列出與其他常見的。即onchange,onclur使用Excel VBA自動化按鍵進入IE瀏覽器按Enter鍵搜索

目前,我使用CreateObject(「Shell.Application」)作爲Excel VBA中的父對象來控制現有的IE瀏覽器。

我也嘗試過sendkeys,但是VBA關注的內容有問題。它在我的IDE或電子表格本身中鍵入。

這不是一個公共站點。它是一個帶有事件(模糊,更改,焦點,keydown,mousedown,mousemove)的輸入標籤。

With Tex_Box 
     .focus 
     .keydown 
     .innertext = Field_Text 
     .change 
     .focus 
     .blur 
    End With 

任何幫助將是偉大的! 在此先感謝。

+0

這是一個有趣的問題,但更多的代碼(最好足以使它成爲[mcve])將有所幫助。 –

+0

對不起,我不知道任何其他網站,這是公開的,例如。 –

+0

現在看。似乎「按Enter鍵搜索」是啓動事件的無按鈕方法。 –

回答

0

因此,stackoverflow.com/questions/11655591/...確實重申了在使用Sendkeys之前如何給予Internet Explorer焦點。然而,這只是謎題的一部分,我仍然需要它專注於文本字段,然後按Enter鍵。由於不知道需要多少標籤才能找到正確的字段,我只是創建了一個循環繼續前進,直到部分字符串「焦點」位於文本字段的類名稱中。一旦發生這種情況,我將它發送給「〜」。

Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) 
Public Declare Function SetForegroundWindow Lib "user32" (ByVal HWND As Long) As Long 
Dim HWNDSrc As Long 

text_again: 
With DOCK_Tex_Box 
    .focus 
    .keydown 
    .innertext = Field_Text 
    .change 
    Sleep (500) 
    .focus 
    .blur 
    HWNDSrc = IE.HWND 
    For i = 1 To 100 
     DoEvents 
     SetForegroundWindow HWNDSrc 
     DoEvents 
     Application.SendKeys ("{TAB}"), True 
     DoEvents 
     Sleep 1000 
     If InStr(.classname, "prompt") > 0 Then 'If the for loop goes to quickly the field loses focus and then the text clears out. 
      DoEvents 
      Sleep 500 
      GoTo text_again: 
     End If 

     If InStr(.classname, "focus") > 0 Then 'Text field has focus 
     Sleep 1000 
     Exit For 
     End If 
    Next 
End With 
Application.SendKeys ("~"), True 
DoEvents 
Sleep 500