将VBA指向IE实例的第二个选项卡

时间:2013-06-07 17:23:52

标签: vba internet-explorer

问题是这一行:

Set TrackID2 = oIE.document.getelementbyid("main-search-box")

基本上,我需要VBA打开一个站点的两个实例(在2个选项卡中),找到搜索框并输入&提交一个字符串。

我打开了标签,但是我找不到VBA将正确的字符串输入IE实例中的第二个标签的方法。是的,现在它打开2个选项卡,但是将两个字符串输入到第一个选项卡中并在那里搜索两次,而第二个选项卡只打开地址。

如何编辑代码以将VBA指向第二个标签而不是第一个标签,就像现在一样?

提前感谢您的帮助!

Sub TestIE() 

Dim IE As Object 
Dim TrackID As Object 
Dim TrackID2 As Object 
Dim address As String 

address = "https://www.google.com/" 

Const navOpenInNewTab = &H800 
Set oIE = CreateObject("InternetExplorer.Application") 
oIE.Visible = True 


oIE.Navigate address 
Do Until oIE.readystate = 4: 
    DoEvents: Loop 
    Set TrackID = oIE.document.getelementbyid("main-search-box") 
    TrackID.Value = ActiveSheet.Range("a1") 
    TrackID.form.submit 

    oIE.Navigate2 address, CLng(navOpenInNewTab) 
Do Until oIE.readystate = 4: 
        DoEvents: Loop 
        Set TrackID2 = oIE.document.getelementbyid("main-search-box") 
        TrackID2.Value = ActiveSheet.Range("A2") 
        TrackID.form.submit 

    End Sub 

1 个答案:

答案 0 :(得分:0)

这是我决定做的事情,因为所有其他选择都超出了我的专业水平......

Sub testie2()


Dim oIE As Object
Dim oIE1 As Object
Dim oIE2 As Object
Dim oIE3 As Object
Dim oIE4 As Object
Dim oIE5 As Object
Dim oIE6 As Object
Dim oIE7 As Object
Dim oIE8 As Object
Dim oIE9 As Object

Dim TrackID As Object
Dim TrackID1 As Object
Dim TrackID2 As Object
Dim TrackID3 As Object
Dim TrackID4 As Object
Dim TrackID5 As Object
Dim TrackID6 As Object
Dim TrackID7 As Object
Dim TrackID8 As Object
Dim TrackID9 As Object

'----------------------------0---------------------------------------------
Set oIE = CreateObject("InternetExplorer.Application")
oIE.Navigate ("https://www.linkedin.com/")
oIE.Visible = True


Do Until oIE.ReadyState = 4:
DoEvents: Loop
Set TrackID = oIE.document.getelementbyid("main-search-box")
TrackID.Value = ActiveSheet.Range("A1")

TrackID.form.submit

'------------------------------1------------------------------------------

Set oIE1 = CreateObject("InternetExplorer.Application")
oIE1.Navigate ("https://www.linkedin.com/")
oIE1.Visible = True

Do Until oIE1.ReadyState = 4:
DoEvents: Loop
Set TrackID1 = oIE1.document.getelementbyid("main-search-box")
TrackID1.Value = ActiveSheet.Range("A2")

TrackID1.form.submit

'------------------------------2-------------------------------------------

Set oIE2 = CreateObject("InternetExplorer.Application")
oIE2.Navigate ("https://www.linkedin.com/")
oIE2.Visible = True

Do Until oIE2.ReadyState = 4:
DoEvents: Loop
Set TrackID2 = oIE2.document.getelementbyid("main-search-box")
TrackID2.Value = ActiveSheet.Range("A3")

TrackID2.form.submit

'------------------------------3-------------------------------------------

Set oIE3 = CreateObject("InternetExplorer.Application")
oIE3.Navigate ("https://www.linkedin.com/")
oIE3.Visible = True

Do Until oIE3.ReadyState = 4:
DoEvents: Loop
Set TrackID3 = oIE3.document.getelementbyid("main-search-box")
TrackID3.Value = ActiveSheet.Range("A4")

TrackID3.form.submit

'------------------------------4-------------------------------------------

Set oIE4 = CreateObject("InternetExplorer.Application")
oIE4.Navigate ("https://www.linkedin.com/")
oIE4.Visible = True

Do Until oIE4.ReadyState = 4:
DoEvents: Loop
Set TrackID4 = oIE4.document.getelementbyid("main-search-box")
TrackID4.Value = ActiveSheet.Range("A5")

TrackID4.form.submit

'------------------------------5-------------------------------------------

Set oIE5 = CreateObject("InternetExplorer.Application")
oIE5.Navigate ("https://www.linkedin.com/")
oIE5.Visible = True

Do Until oIE5.ReadyState = 4:
DoEvents: Loop
Set TrackID5 = oIE5.document.getelementbyid("main-search-box")
TrackID5.Value = ActiveSheet.Range("A6")

TrackID5.form.submit

'------------------------------6-------------------------------------------

Set oIE6 = CreateObject("InternetExplorer.Application")
oIE6.Navigate ("https://www.linkedin.com/")
oIE6.Visible = True

Do Until oIE6.ReadyState = 4:
DoEvents: Loop
Set TrackID6 = oIE6.document.getelementbyid("main-search-box")
TrackID6.Value = ActiveSheet.Range("A7")

TrackID6.form.submit

'------------------------------7-------------------------------------------

Set oIE7 = CreateObject("InternetExplorer.Application")
oIE7.Navigate ("https://www.linkedin.com/")
oIE7.Visible = True

Do Until oIE7.ReadyState = 4:
DoEvents: Loop
Set TrackID7 = oIE7.document.getelementbyid("main-search-box")
TrackID7.Value = ActiveSheet.Range("A8")

TrackID7.form.submit

'------------------------------8-------------------------------------------

Set oIE8 = CreateObject("InternetExplorer.Application")
oIE8.Navigate ("https://www.linkedin.com/")
oIE8.Visible = True

Do Until oIE8.ReadyState = 4:
DoEvents: Loop
Set TrackID8 = oIE8.document.getelementbyid("main-search-box")
TrackID8.Value = ActiveSheet.Range("A9")

TrackID8.form.submit

'------------------------------9-------------------------------------------

Set oIE9 = CreateObject("InternetExplorer.Application")
oIE9.Navigate ("https://www.linkedin.com/")
oIE9.Visible = True

Do Until oIE9.ReadyState = 4:
DoEvents: Loop
Set TrackID9 = oIE9.document.getelementbyid("main-search-box")
TrackID9.Value = ActiveSheet.Range("A10")

TrackID9.form.submit


End Sub

我知道它不优雅,但是减少了很多手动输入,绝对有助于完成这项工作,但肯定希望能将所有这些都融入到标签中。