Tab Across to an IE Tab using VBA

自闭症网瘾萝莉.ら 提交于 2019-12-11 14:50:23

问题


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.

Please select version of Excel
(o) Excel 2000
(o) Previous versions

Ok Cancel

But I can not set focus to the newly created IE Tab after having clicked the hyperlink from the initial page.

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

回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/21050183/tab-across-to-an-ie-tab-using-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!