VBA:在 Internet Explorer 上选择特定选项卡 [英] VBA: Choosing Specific Tab on Internet Explorer

查看:39
本文介绍了VBA:在 Internet Explorer 上选择特定选项卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里发布了另一篇关于同一整体问题的不同问题的帖子:从早期绑定转换为晚期绑定.我的编码现在有一个新问题(我将在下面发布其中的一部分),如果我打开带有多个选项卡的 Internet Explorer,我的代码将不再填充文本框 - 即使选项卡是当前正在查看的.一旦我关闭所有其他选项卡,代码就会完美运行.

I had another post regarding a different issue on the same overall problem here: Converting From Early Binding to Late Binding. I now have a new issue with my coding (which I will post a portion of it below), where if I have Internet Explorer open with multiple tabs, my code no longer fills in the text boxes - even if the tab is the one currently being viewed. As soon as I close all other tabs, the code runs flawlessly.

如果标签在 URL: https://sub.website.com/dir/ 上被命名为 Tab1,我怎样才能在此填写表格有多个标签的网站?

If the tab was named Tab1 at the URL: https://sub.website.com/dir/, how can I have the forms filled out on this site with multiple tabs?

这是正在使用的代码(由 cyboashu 提供,并来自 蒂姆·威廉姆斯):

Here is the code being used (courtesy of cyboashu and help from Tim Williams):

Sub Test()
 ' Code Cut Here
    Dim oShell      As Object
    Dim oWin        As Object
    Dim IE          As Object
    Dim lTotlWin    As Long
    Dim lCtr

    Debug.Print Time & " --- IE Objects & Values ---"       ' Debugger Section
    Set oShell = CreateObject("Shell.Application")
        Debug.Print Time & " [obj ] oShell..: " & oShell    ' Debug oShell
    Set oWin = oShell.Windows()
        Debug.Print Time & " [obj ] oWin....: " & oWin      ' Debug oWin

    lTotlWin = oWin.Count - 1   '/ Starts with zero
    Debug.Print Time & " [long] lTotlWin: " & lTotlWin      ' Debug lTotlWin

    For lCtr = 0 To lTotlWin
        If UCase(oWin.Item(lCtr).FullName) Like "*IEXPLORE.EXE" Then
            Set IE = oWin.Item(lCtr)
        End If
    Next
    Debug.Print Time & " [obj ] IE......: " & IE
    If Not IE Is Nothing Then
        MsgBox "Found and hooked!!"
    End If

    Dim TBox As String
    Dim TBtn As String
        TBox = "masked1"
        Tbtn = "button"


    If Not IE Is Nothing Then
        Set txtBox = IE.Document.getElementsByClassName(TBox)(0)
        Debug.Print Time & " [obj ] txtbox..: " & txtbox
        Set submitBtn = IE.Document.getElementsByClassName(Tbtn)(4)
        Debug.Print Time & " [obj ] submitBtn:" & submitBtn

        txtBox.Value = tVal
        submitBtn.Click
    End If
End Sub

推荐答案

这是我在自动化现有 IE 窗口时通常使用的:

Here's what i typically use when automating an existing IE window:

Sub Tester()
     Dim IE As Object
     Set IE = GetIE("http://www.google.com")
     Debug.Print IE.document.Title
     'work with IE
End Sub


Function GetIE(sLocation As String) As Object

    Dim objShell As Object, objShellWindows As Object, o As Object
    Dim sURL As String
    Dim retVal As Object

    Set retVal = Nothing
    Set objShell = CreateObject("Shell.Application")
    Set objShellWindows = objShell.Windows

    For Each o In objShellWindows
        sURL = ""
        On Error Resume Next  'because may not have a "document" property
        'Check the URL and if it's the one you want then
        ' assign the window object to the return value and exit the loop
        sURL = o.document.Location
        On Error GoTo 0
        If sURL Like sLocation & "*" Then
            Set retVal = o
            Exit For
        End If
    Next o

    Set GetIE = retVal

End Function

这篇关于VBA:在 Internet Explorer 上选择特定选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆