使用 VBA 切换到 IE 选项卡
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21050183/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Tab Across to an IE Tab using VBA
提问by mond007
I have VBA code that launches a webpage, searches for a hyperlink, clicks the hyperlink to open another IE tab that asks what version of Excel to view that data in.
我有启动网页的 VBA 代码,搜索超链接,单击超链接以打开另一个 IE 选项卡,该选项卡询问要查看该数据的 Excel 版本。
Please select version of Excel
(o) Excel 2000
(o) Previous versionsOk Cancel
请选择 Excel 版本
(o) Excel 2000
(o) 以前的版本确定 取消
But I can not set focus to the newly created IE Tab after having clicked the hyperlink from the initial page.
但是从初始页面单击超链接后,我无法将焦点设置到新创建的 IE 选项卡。
The code below so far works well.
到目前为止,下面的代码运行良好。
Private Sub CommandButton1_Click()
Dim winShell As Shell
Dim ie As Object
Dim ie_window As Variant
Dim target_URL As String
Dim ieApp As Object
Dim ieDoc As Object
Dim ieForm As Object
Dim ieObj As Object
Dim lnk
target_URL = Worksheets("Control Panel").Range("B3").Value
no_of_tabs_to_hit = Worksheets("Control Panel").Range("B5").Value
With CreateObject("Shell.Application").Windows
If .Count > 0 Then
Set ie = .Item(0) '.Item(.Count + 1) ' Get IE
Else
Set ie = CreateObject("InternetExplorer.Application") ' Create IE
ie.Visible = True
End If
ie.Navigate target_URL
ie.Visible = True
End With
ie.Visible = True
'----------------------- WAIT FOR IE TO CATCH UP ----------------------------
newHour = Hour(Now())
newMinute = Minute(Now())
newSecond = Second(Now()) + 4
waitTime = TimeSerial(newHour, newMinute, newSecond)
Application.Wait waitTime
'----------------------------------------------------------------------------
Set winShell = New Shell 'loop through the windows and look at the urls
For Each ie In winShell.Windows
If InStr(1, ie.LocationURL, "PT2200JC", vbString) > 0 Then
ie.Visible = True 'GetInternetWindow = ie
'AppActivate ie
Exit For
End If
Next
'----------------------------------------------------------------------------
Set doc = ie.Document
For Each lnk In doc.Links
If InStr(1, lnk.innerHTML, "View in Excel", vbTextCompare) > 0 Then
'MsgBox "Links on page are : " & lnk.innerHTML
lnk.Click
End If
Next lnk
'Application.SendKeys "^~" ' 22
'Set ie = Nothing
End Sub
回答by mond007
Many thanks for your I used the above to get to the correct page and I managed to find far better way to click the hyperlink.
非常感谢您的帮助,我使用上面的方法访问了正确的页面,并且我设法找到了更好的方法来单击超链接。
Set doc = ie.Document
For Each lnk In doc.Links
If InStr(1, lnk.innerHTML, "View in Excel", vbTextCompare) > 0 Then
'MsgBox "Links on page are : " & lnk.innerHTML
lnk.Click
End If
Next lnk
Thanks very much Kuldip.
非常感谢库尔迪普。
回答by ron
Something like this should help you identify and focus on the desired web page
像这样的东西应该可以帮助您识别并专注于所需的网页
Set objShell = CreateObject("Shell.Application")
IE_count = objShell.Windows.Count
For x = 0 To (IE_count - 1)
On Error Resume Next ' sometimes more web pages are counted than are open
my_url = objShell.Windows(x).Document.Location
my_title = objShell.Windows(x).Document.Title
'find the desired page
If my_title Like "Put something from your IE page title here" & "*" Then
Set ie = objShell.Windows(x)
Exit For
Else
End If
Next