奇怪的是,我没有找到关于该主题的任何信息,而且我目前陷入困境,我通过编程方式单击按钮设法在IE实例中打开一个新选项卡,但我没有最微弱的关于如何处理新标签以获取信息的线索(该按钮基本上会显示一个带有搜索结果的新标签页)。
这基本上是一个简单的问题,但我仍然包括我的代码:
Sub AddInfoFromIntranet()
Dim Ie As SHDocVw.InternetExplorer
Dim URL As String
Dim iFrames As MSHTML.IHTMLElementCollection
Dim iFrame As MSHTML.HTMLFrameElement
Dim Doc As MSHTML.HTMLDocument
Dim InputBox As MSHTML.IHTMLElementCollection, htmlButton, allTags, Tag
' Opens Intranet - yeah, sadly it's not a public web page
URL = "{My intranet website}"
Set Ie = New SHDocVw.InternetExplorer
With Ie
.navigate URL
.Visible = True
While .Busy Or .readyState <> READYSTATE_COMPLETE: DoEvents: Wend
Set Doc = .document
End With
' Gets top_window frame and navigates to it, then inserts the name to search
Set iFrames = Doc.getElementsByName("top_window")
If Not iFrames Is Nothing Then
Set iFrame = iFrames(0)
Ie.navigate URL & iFrame.src
While Ie.Busy Or Ie.readyState <> READYSTATE_COMPLETE: DoEvents: Wend
Set InputBox = Doc.getElementsByName("Nachnamevalue")
If Not InputBox Is Nothing Then InputBox(0).Value = "test"
' Clicks on "search"
Set allTags = Doc.getElementsByTagName("input")
For Each Tag In allTags
If Tag.Value = "suchen" Then
Tag.Click
Exit For
End If
Next
' Here a new tab is opened, must find info in this tab
While Ie.Busy Or Ie.readyState <> READYSTATE_COMPLETE: DoEvents: Wend
' HERE I HAVE NO CLUE WHAT TO WRITE. THE CODE ABOVE WORKS FLAWLESSLY
End If
Set Doc = Nothing
Set iFrames = Nothing
Set iFrame = Nothing
Set InputBox = Nothing
Set allTags = Nothing
Set Ie = Nothing
Ie.Quit
End Sub
现在,有没有办法通过以下方式来处理标签:1)它的名称(以及我在哪里找到它)2)它在浏览器中的位置3)状态(如果它是“活动的”)?
奖金问题:由于我不熟悉VBA和Internet Explorer互动,究竟有哪些变量:htmlButton, allTags, Tag
?另外,任何人都可以解释我是否需要将所有变量设置为空,或者我只需要将Internet Explorer设置为空?
提前致谢!
答案 0 :(得分:3)
请参阅下面的可用于获取打开的IE文档窗口的函数 - 我不认为IE公开任何简单(VBA可访问)API以直接使用选项卡或确定特定选项卡是否处于活动状态。
allTags
是DOM元素的集合,类型为&#34;&#34; ,Tag
是该系列的唯一成员。
在退出Sub之前,你没有 将对象设置为Nothing(虽然有些人仍然这样做) - VBA运行时会为你处理。
Sub TestGetIE()
Dim IE As Object
Set IE = GetIE("http://stackoverflow.com")
If Not IE Is Nothing Then
IE.document.execCommand "Print", False, 0
End If
End Sub
'Get a reference to an open IE window based on its URL
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
'check the URL and if it's the one you want then
' assign it to the return value
sURL = o.document.Location
On Error GoTo 0
'Debug.Print sURL
If sURL Like sLocation & "*" Then
Set retVal = o
Exit For
End If
Next o
Set GetIE = retVal
End Function