2012-04-17 103 views
6

正嘗試從Microsoft Access 2003中使用自動化來控制Internet Explorer 9以使用數據庫數據完成表單。使用VBA輸入事件的Internet Explorer自動化

輸入在瀏覽器中觸發一個事件來驗證數據並使保存按鈕可見。如果我使用sendkeys事件被觸發;不過,我發現sendkeys非常不可靠。如果我更改元素的值,然後使用.fireevent(「onchange」),則不會發生任何事情 - 甚至不會發生錯誤。

我的問題是,我該如何解決這個問題。或者,我怎樣才能找出什麼是JavaScript運行。是否有一個調試類型的IE插件,它會告訴我什麼事件被解僱?如果是這樣,我可以自己運行腳本嗎?

我的代碼如下。

Set IE = CreateObject("internetexplorer.application") 
IE.Visible = True 
IE.Navigate "https://extranet.website.com/Planning/Edition/Periodic?language=en" 

Do While IE.ReadyState <> 4 Or IE.Busy = True 
    DoEvents 
Loop 


'log in 
If IE.Document.Title = "website- access" Then 
    IE.Document.getElementById("login_uid").Value = "username" 
    IE.Document.getElementById("login_pwd").Value = "password" 
    IE.Document.all("ButSubmit").Click 
    Do While IE.ReadyState <> 4 Or IE.Busy = True 
     DoEvents 
    Loop 
End If 

Do While Not RstAvailability.EOF 
    StartDate = RstAvailability!AvailDate 
    IE.Document.getElementById("periodStart").Value = Format(StartDate, "dd mmm yy") 
    IE.Document.getElementById("periodEnd").Value = Format(StartDate, "dd mmm yy") 
    Set LinkCollection = IE.Document.GetElementsByTagName("A") 
    For Each link In LinkCollection 
     If link.innertext = "Add" Then 
      link.Click 
      Exit For 
     End If 
    Next 
    Do While IE.ReadyState <> 4 Or IE.Busy = True 
     DoEvents 
    Loop 


    Set objRows = IE.Document.GetElementsByTagName("tr") 
    If RstAvailability!RoomType = "DTW" Then 
     n = 0 
     While n < objRows.Length 
      If Trim(objRows(n).Cells(0).innertext) = "Single Room" Then 
       For i = 1 To 7 
        'objRows(n).FireEvent ("onchange") 
        'objRows(n).Cells(i).GetElementsByTagName("input")(0).Focus 
        'SendKeys Format(RstAvailability!roomcount - RstAvailability!RoomsSold, "0") & "{TAB}" 
        objRows(n).Cells(i).GetElementsByTagName("input")(0).Value = Format(RstAvailability!roomcount - RstAvailability!RoomsSold, "0") 
        objRows(n).Cells(i).GetElementsByTagName("input")(0).fireevent ("onchange") 
        Do While IE.ReadyState <> 4 Or IE.Busy = True 
         DoEvents 
        Loop 
       Next i 
      End If 
      n = n + 1 
     Wend 
    End If 

    Set objButtons = IE.Document.getelementsbyname("savePlanning") 
    objButtons(0).Click 

    Do While IE.ReadyState <> 4 Or IE.Busy = True 
     DoEvents 
    Loop 

    newtime = Now + TimeValue("0:00:10") 
    Do While True 
     If Now > newtime Then Exit Do 
    Loop 

    RstAvailability.MoveNext 
Loop 

輸入字段的HTML是:

<tr class="first" roomId="30494" articleId="0" type="Availability" readonly="False"> 

<div> 

    <span class="roomName"> 

    Single Room 

    </span> 



</div> 

<span class="data"> 

<input id="Availabilities" name="Availabilities" type="text" value="" /> 

</span> 

<span class="data"> 

<input id="Availabilities" name="Availabilities" type="text" value="" /> 

</span> 

謝謝!

回答

12

在這幾天出汗之後,答案其實非常簡單,但幾乎不可能在任何MSDN或網絡上的其他任何文檔中找到。

在更改輸入字段的值之前,您需要將網絡焦點設置爲該字段。更改該值後,您需要將焦點設置到另一個字段。顯然,這些事件引發了焦點的喪失。因此,代碼應該是這樣的:

 n = 0 
    While n < objRows.Length 
     If Trim(objRows(n).Cells(0).innertext) = "Family Room" Then 
      For i = 1 To 7 
       objRows(n).Cells(i).GetElementsByTagName("input")(0).Focus 
       objRows(n).Cells(i).GetElementsByTagName("input")(0).Value = Format(RstAvailability!roomcount - RstAvailability!RoomsSold, "0") 
      Next i 
      objRows(n).Cells(1).GetElementsByTagName("input")(0).Focus 

     End If 
     n = n + 1 
    Wend 

我發現,這是通過查看有關輔助IE9的一些MSDN文檔的方式。它建議爲殘疾用戶設定焦點。我只是想我會嘗試一下,它會奏效。我希望這可以幫助別人。

戴夫

+0

乾杯這是有益的,我 – BlueTrin 2012-10-10 13:26:40

1

如果你想(在IE9)把下面的行線「下一個我」之前。即使你沒有放鬆焦點,它也應該起作用。

objRows(n)的.Cells(ⅰ).GetElementsByTagName( 「輸入」)(0)。單擊