使用VBA跨到IE选项卡 [英] Tab Across to an IE Tab using VBA

查看:259
本文介绍了使用VBA跨到IE选项卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

确定取消

但是在单击初始页面的超链接后,我无法将焦点设置到新创建的IE选项卡上.

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

推荐答案

非常感谢您使用以上内容访问了正确的页面,并设法找到了一种更好的单击超链接的方法.

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

非常感谢Kuldip.

Thanks very much Kuldip.

这篇关于使用VBA跨到IE选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆