使用VBA选项卡到IE选项卡

时间:2014-01-10 17:04:08

标签: excel vba internet-explorer hyperlink tabs

我有VBA代码,用于启动网页,搜索超链接,单击超链接以打开另一个IE选项卡,询问要查看该数据的Excel版本。

  

请选择Excel版本   (o)Excel 2000
  (o)以前的版本

     

确定取消

但是,在点击初始页面的超链接后,我无法将焦点设置到新创建的IE Tab。

到目前为止,下面的代码效果很好。

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

2 个答案:

答案 0 :(得分:1)

非常感谢您使用上述内容进入正确的页面,我找到了更好的方法来点击超链接。

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

非常感谢Kuldip。

答案 1 :(得分:0)

这样的事情可以帮助您识别并关注所需的网页

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