VBA如何在单个网站内与2个HTML文档进行交互?

时间:2019-04-08 19:13:51

标签: html excel vba web

Here's the main URL Main HTML Document the URL for the button i'm trying to locate 2nd HTML Document that I'm trying to get its handle

我要执行的操作是获取要单击的按钮的句柄。但是,有两个URL,第一个是窗口中的主URL,第二个是窗口内的URL(当您右键单击>属性按钮时,URL与主窗口的URL不同)。另外,它还是第二个HTML文档(请注意:没有iframe / frame。)

只有第二个URL我无法获取该句柄,以便我控制其元素。

我已经在下面尝试了一些代码来获取所有句柄。

    ConnectURL "Secondary URL"
    GetTheWindowHandle "IEFrame", "Secondary URL - Internet Explorer", hwndp
    SetForegroundWindow hwndp

    Sub ConnectURL(strURL As String)
    On Error Resume Next
    Set sws = New SHDocVw.ShellWindows       
    jar = sws.Count - 1
    If jar < 0 Then
        MsgBox "There's no active IE or window Explorer", vbInformation, "Pop-out"
        Set sws = Nothing
        Exit Sub
    End If
    n = 0
    Do Until (n > 5000)
        If (Left(sws.Item(n).LocationURL, Len(strURL)) = strURL) Then
            If err.Number = 0 Then
                Exit Do 
            End If
            err.Clear
        End If

        n = n + 1
    Loop
    Set ieDoc = sws.Item(n).document
    sws.Item(n).Visible = True
    Set ieLoad = sws.Item(n)
    End Sub

    Function GetTheWindowHandle(C`enter code here`lassName As String, toSearch As String, handle As Long)

    'Get Handle of window
        tempHandle = 0
        tempHandle = FindWindowEx(0, tempHandle, ClassName, vbNullString)

        Do While tempHandle <> 0 'while handle is not seen

            GetNameOfWindowByHandle tempHandle

                If InStr(1, TextResult, toSearch) > 0 Then 'Check if Window title is similar
                    handle = tempHandle 'capture handle
                    Exit Do
                End If

            tempHandle = FindWindowEx(0, tempHandle, ClassName, vbNullString)

        Loop
    End Function

    Function GetNameOfWindowByHandle(myHwnd As Long)
    'capture the title of the window
        Dim sbuffer As Long
        Dim lresult As String

        sbuffer = SendMessage(myHwnd, WM_GETTEXTLENGTH, 0, vbNullString)
        sbuffer = sbuffer + 1
        TextResult = Space$(sbuffer)
        lresult = SendMessage(myHwnd, WM_GETTEXT, sbuffer, ByVal TextResult)

    End Function

    Example below
    <div class="slit-item-active" id="reportsTab" onclick="openReportExplorer();"><div class="nav-icon3"></div><span id="reportsTabText">Reports</span></div>

    this html code is on the button with the 2nd URL that I'm trying to get it's handle so i can use my code:

    iedoc.getelementbyid("reportsTab").click

    or

            waiting = 0
            Do While waiting = 0
                For Each obj1 In ieDoc.getElementById("reportsTab").getElementsByTagName("span")
                Debug.Print obj1.innerText
                    If Trim(obj1.innerText) = "Reports" Then
                        obj1.Click
                        ieLoading 2000
                        waiting = 1
                        Exit For
                    End If
                Next
            Loop

1 个答案:

答案 0 :(得分:0)

click事件不适用于该Div元素/标签。您需要改为触发onclick事件。

iedoc.getelementbyid("reportsTab").FireEvent "onclick"