VBA:在Internet Explorer上选择“特定"选项卡 [英] VBA: Choosing Specific Tab on 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名称为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屋!