代码之家  ›  专栏  ›  技术社区  ›  Michael

点击按钮或用VBA执行JavaScript函数

  •  3
  • Michael  · 技术社区  · 15 年前

    我正在尝试让我的VBA例程与使用JavaScript的页面交互,并且在过去取得了成功。经过最近的更新后,它不再工作。此时,我需要A)以编程方式单击按钮,或者B)执行按钮调用的函数。棘手的是按钮没有声明名称。在源代码中声明的表单上还有其他名称,我可以很好地与它们交互。以下是页面源代码中的html代码:

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

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

    ie.document.all(81).Click
    

    因为某种原因不起作用。我知道要执行的函数:exportData(workOrderSearchForm),但除了使用“click”方法之外,我不知道如何执行。

    我已经找了一些关于IE应用程序对象的像样的文档,但似乎找不到好的源代码。有办法执行这个函数吗?

    3 回复  |  直到 7 年前
        1
  •  11
  •   stealthyninja michkra    13 年前

    试试这个:

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

    但首先您需要在References下包含Microsoft HTML对象库(Tools>(参考文献)

        2
  •  2
  •   MikeD    14 年前

    您可以循环浏览所有输入标记,并通过检查“最具识别性”属性(id、name、type、innerHTML等等)来识别相关的标记。下面是我在Excel工作表中使用的Sub(),它可以自动登录到网站

    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
    

    函数采用以下参数:

    • 标签。。。。要处理的标记(通常是input、image、a…)
    • 匹配。。。。属性的匹配值
    • 行动。。。。。如果对匹配的标记执行“/C/”.Click()操作,则操作值将放入标记的value属性中

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

    如果要挂接例如“exportData(workOrder)”,则需要使用Instr()函数或类似函数来考虑这一点。

    我成功的原因是:

    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
    

        3
  •  0
  •   SLaks    15 年前

    这样地:

    ie.window.exportData(ie.document.forms.workOrderSearchForm)
    
    推荐文章