2012-04-17 39 views
6

đang cố gắng sử dụng tự động hóa từ Microsoft Access 2003 để kiểm soát Internet Explorer 9 để hoàn tất biểu mẫu bằng cách sử dụng dữ liệu cơ sở dữ liệu.Tự động hóa Internet Explorer với sự kiện đầu vào VBA

Đầu vào kích hoạt sự kiện trong trình duyệt xác thực dữ liệu và làm cho nút lưu hiển thị. Nếu tôi sử dụng sendkeys, sự kiện được kích hoạt; tuy nhiên, tôi đã tìm thấy những con lừa rất không đáng tin cậy. Nếu tôi thay đổi giá trị của phần tử và sau đó sử dụng .fireevent ("onchange"), không có gì xảy ra - thậm chí không có lỗi.

Câu hỏi của tôi là, làm cách nào để kích hoạt sự kiện. Hoặc, làm thế nào tôi có thể tìm ra javascript đang chạy. Có một loại gỡ lỗi của addin cho IE mà sẽ cho tôi biết những gì sự kiện được bắn? Nếu vậy, tôi có thể tự chạy kịch bản không?

Mã của tôi ở bên dưới.

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 

Các html của các lĩnh vực đầu vào là:

<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> 

Cảm ơn!

Trả lời

12

Sau khi đổ mồ hôi trong vài ngày, câu trả lời thực sự rất đơn giản nhưng gần như không thể tìm thấy trong bất kỳ tài liệu nào trên MSDN hoặc bất kỳ nơi nào khác trên web.

Trước khi bạn thay đổi giá trị của trường nhập, bạn phải đặt trọng tâm vào trường đó. Sau khi bạn thay đổi giá trị, bạn cần đặt trọng tâm cho trường khác. Rõ ràng, các sự kiện cháy trên sự mất tập trung. Do đó, mã phải giống như sau:

 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 

Cách tôi tìm thấy điều này bằng cách xem một số tài liệu MSDN về trợ năng cho IE9. Đó là tư vấn về việc đặt trọng tâm cho người dùng bị vô hiệu hóa. Tôi chỉ nghĩ rằng tôi sẽ cung cấp cho một thử và nó đã làm việc. Tôi mong điều này giúp được người nào khác.

Dave

+0

Cheers đó là hữu ích cho tôi – BlueTrin

1

Nếu bạn muốn (trong IE9) để đưa dòng sau ngay trước dòng "Next i". nó cũng sẽ hoạt động ngay cả khi bạn không mất tập trung?

objRows (n) .Cells (i) .GetElementsByTagName ("đầu vào") (0) Tích vào

Các vấn đề liên quan