Reputation: 109
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 versionsOk 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
Upvotes: 0
Views: 9454
Reputation: 109
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.
Upvotes: 1
Reputation: 1378
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
Upvotes: 0