javascript
  • vba
  • excel-vba
  • 2010-07-14 63 views 3 likes 
    3

    我正試圖讓我的VBA例程與使用JavaScript的頁面進行交互,並在過去取得了成功。最近更新後,它不再有效。此時我需要A)以編程方式單擊按鈕或B)執行按鈕調用的功能。棘手的部分是該按鈕沒有聲明的名稱。在表單中還有其他人在源代碼中聲明瞭名稱,我可以很好地與它們交互。以下是來自頁面源代碼的html代碼:單擊按鈕或使用VBA執行JavaScript功能

    <input type="button" class="buttonForm" value='Run Report' onclick="exportData(workOrderSearchForm)"/> 
    

    正如您所看到的,它沒有聲明該按鈕的名稱。在過去,以下工作:

    Set ie = CreateObject("InternetExplorer.application") 
    ie.document.all(79).Click 
    

    「79」是項目編號的索引。現在看來,該按鈕已更改爲項目「81」,只是投入:

    ie.document.all(81).Click 
    

    工作不因某種原因。我知道我想要執行的函數:exportData(workOrderSearchForm),但不知道如何在使用「click」方法之外這麼做。

    我已經找了一些關於IE應用程序對象的體面的文檔,但似乎無法找到一個好的來源。有沒有辦法執行該功能?

    回答

    8

    試試這個:

    Dim CurrentWindow As HTMLWindowProxy: Set CurrentWindow = ie.Document.parentWindow 
    Call CurrentWindow.execScript("exportData(workOrderSearchForm)") 
    

    但首先你需要包括在參考文獻(工具>參考)Microsoft HTML對象庫

    0

    像這樣:

    ie.window.exportData(ie.document.forms.workOrderSearchForm) 
    
    +0

    不幸的是,它說這不是一個支持的方法或屬性。 「if.window.exportData(ie.Document.forms.workOrderSearchForm)」一旦放入,它也將代碼間隔爲空。 – Michael 2010-07-14 15:24:29

    +0

    嘗試'ie.window.exportData ie.window.workOrderSearchForm'或'ie.window.eval 「exportData(workOrderSearchForm)」' – SLaks 2010-07-14 15:25:27

    +0

    這兩者似乎都無法做到這一點。他們產生相同的錯誤。 – Michael 2010-07-14 15:31:59

    1

    可以循環通所有輸入標籤,並通過檢查「最具識別性」屬性(id,name,type,innerHTML,... whatever)來識別相關標籤。這裏的一個小組()我在Excel工作表可以自動登錄到網站使用

    Sub FormAction(Doc As MSHTML.HTMLDocument, ByVal Tag As String, ByVal Attrib As String, ByVal Match As String, ByVal Action As String) 
    Dim ECol As MSHTML.IHTMLElementCollection 
    Dim IFld As MSHTML.IHTMLElement 
    Dim Tmp As String 
    
        Set ECol = Doc.getElementsByTagName(Tag) 
        For Each IFld In ECol           ' cycle thru all <[tag]> elements 
         If VarType(IFld.getAttribute(Attrib)) <> vbNull Then  ' does it contain the attribute 
          If Left(IFld.getAttribute(Attrib), Len(Match)) = Match Then 
           If Action = "/C/" Then 
            IFld.Click 
           Else 
            IFld.setAttribute "value", Action 
           End If 
           Exit Sub 
          End If 
         End If 
        Next 
    End Sub 
    

    該函數採用以下參數:

    • 文件....的HTML DOM對象
    • 標籤....你想要處理的標籤(通常是輸入,圖像,a,...)
    • 屬性....你想要匹配的值的屬性
    • 匹配....匹配值爲屬性
    • 行動.....如果在匹配標籤進行「/ C /」的。點擊()動作,其他動作值放入變量的值屬性

    如果你想勾上包含的JavaScript代碼(如「點擊」)不要忘記,你的HTML源看到代碼在一個匿名函數內嵌任何屬性,就像

    function anonymous() 
    { 
    onclick="exportData(workOrderSearchForm)"; 
    } 
    

    您需要使用的可以考慮一下Instr()函數或類似的,如果你想掛鉤例如「exportData(workOrder)」。

    也非常重要:給頁面足夠的時間導航到DOM對象並加載。我注意到我的公司,一旦頁面被更改,加載時間有時會大幅增加。

    我有這個良好的成功:

    Sub MyMainSub() 
    Dim Browser As SHDocVw.InternetExplorer 
    Dim HTMLDoc As MSHTML.HTMLDocument 
    ' my other Dim's 
    
        ' start browser 
        Set Browser = New SHDocVw.InternetExplorer 
        Browser.navigate "http://www.whatever.com" 
        WaitForBrowser Browser, 5   ' wait for browser, but not more than 5 sec 
    
        ' gain control over DOM object 
        Set HTMLDoc = Browser.document 
        WaitForBrowser Browser, 5   ' wait for browser, but not more than 5 sec 
    
        ' do actions 
        ' FormAction HTMLDoc, w, x, y, z 
    
    End Sub 
    
    
    Sub WaitForBrowser(Browser As SHDocVw.InternetExplorer, Optional TimeOut As Single = 10) 
    Dim MyTime As Single 
    
        MyTime = Timer     ' seconds since midnight 
        Do While Browser.Busy Or (Timer <= MyTime + TimeOut) 
         DoEvents      ' go do something else 
        Loop 
    
        If Browser.Busy Then 
         MsgBox "I waited for " & Timer - MyTime & " seconds, but browser still busy" & vbCrLf & _ 
           "exititing Login sequence now" 
         End 
        End If 
    End Sub 
    

    希望這是鼓舞人心的足夠爲您打造您的解決方案。

    祝你好運MikeD

    相關問題