2013-05-13 84 views
5

使用VBA我可以使用以下访问IE选项卡一旦创建

Option Explicit 

Public Enum IE_READYSTATE 
    Uninitialised = 0 
    Loading = 1 
    Loaded = 2 
    Interactive = 3 
    complete = 4 
End Enum 

Sub Example_Click() 
Dim ieApp As clsIE 

    'Create IE and login 
    If ieApp Is Nothing Then 
     Set ieApp = New clsIE 
     With ieApp 

      'IE Tab1 
      .IE.Visible = True 
      .IE.navigate "http://www.bbc.co.uk/news/" 
      Do While .IE.Busy Or Not .IE.readyState = IE_READYSTATE.complete: DoEvents: Loop 

      'IE Tab2 
      .IE.Navigate2 "http://www.bbc.co.uk", CLng(2048) 
      Do While .IE.Busy Or Not .IE.readyState = IE_READYSTATE.complete: DoEvents: Loop 

      'IE Tab3 
      .IE.Navigate2 "http://www.bbc.co.uk", CLng(2048) 
      Do While .IE.Busy Or Not .IE.readyState = IE_READYSTATE.complete: DoEvents: Loop 


     End With 
    End If 

End Sub 

我怎样才能然后访问这些标签来创建....用3页不同的选项卡的InternetExplorer对象

  1. 退出/关闭特定标签?
  2. 导航到特定选项卡上的新URL?
  3. 访问特定标签DOM?

我知道如何做到这一切与单个标签,但不是多个标签?

回答

4

我发现了一个似乎可行的答案,尽管我没有做过严格的测试。我修改了你的代码,这样我就可以在没有你的clsIE模块的情况下运行它。此代码在打开它们时将标签导航到3个URL,然后使用shellwindows对象将它们导航到新的URL。

我改变了做,而.busy ..行,因为他们没有工作的第二和第三个标签在IE浏览器applecation的状态准备好,而新的标签仍在加载。

Sub Example_Click() 
Dim ieApp As InternetExplorer 
Dim SWs As ShellWindows 
Dim IETab1Number As Integer 
Dim IETab2Number As Integer 
Dim IETab3Number As Integer 

Set SWs = New ShellWindows 

'Create IE and login 
If ieApp Is Nothing Then 
    Set ieApp = CreateObject("InternetExplorer.Application") 
    With ieApp 

     'IE Tab1 
     .Visible = True 
     .Navigate "http://www.bbc.co.uk/news/" 
     Do While .Busy Or Not .ReadyState = IE_READYSTATE.complete: DoEvents: Loop 
     IETab1Number = SWs.Count 

     'IE Tab2 
     .Navigate2 "http://www.bbc.co.uk/news/uk-scotland-north-east-orkney-shetland-23822420", CLng(2048) 
     'Do While .Busy Or Not .ReadyState = IE_READYSTATE.complete: DoEvents: Loop 
     Do While SWs.Count = IETab1Number: DoEvents: Loop 

     IETab2Number = SWs.Count 

     'IE Tab3 
     .Navigate2 "http://www.bbc.co.uk", CLng(2048) 
     'Do While .Busy Or Not .ReadyState = IE_READYSTATE.complete: DoEvents: Loop 
     Do While SWs.Count = IETab2Number: DoEvents: Loop 
     IETab3Number = SWs.Count 
     'ieApp.Visible = False 

     SWs.Item(IETab1Number - 1).Navigate "http://www.bbc.co.uk/" 
     SWs.Item(IETab2Number - 1).Navigate2 "http://www.bbc.co.uk/news/" 
     SWs.Item(IETab3Number - 1).Navigate2 "http://www.bbc.co.uk/news/" 


    End With 
End If 
Set ieApp = Nothing 
Set SWs = Nothing 

End Sub 

它使用ShellWindows来操纵选项卡。这只能通过数字来完成,所以我猜测它可能容易出错。

为了使它更强大,您可能希望在操作后从标签中获取一些信息,并在返回时检查该值是否相同。例如sPageTitle = SWs.Item(IETab3Number - 1).Document.Title可用于在标签中存储页面的标题,下次您想要使用标签时,可以检查它没有改变。

+1

将+5,如果我能:) – user3357963 2013-08-24 11:26:44

1

这里是我如何解决在VBScript上的一个IE窗口中分组标签的问题。/tabGroupS /是不适用的。如“http://stackoverflow.com”。如果函数找到带有打开的stackoverflow选项卡的IE窗口,则会向该窗口添加一个新选项卡,并返回此选项卡,否则会打开新窗口。

Private Function getNewTab(tabGroupS) 
    Dim i, objs, cnt, openflag 
    Set objs = CreateObject("Scripting.Dictionary") 
    Set SWObj = CreateObject("Shell.Application").Windows() 
    For Each i In SWObj 
     If LCase(Right(i.FullName, 12)) = "iexplore.exe" Then objs.Add objs.Count, i 
    Next 
    cnt = SWObj.Count 
    For Each i In objs 
     If Left(objs(i).LocationURL, Len(tabGroupS)) = tabGroupS Then 
      openflag = True 
      objs(i).Navigate2 "about:blank", &H800 
      Exit For 
     End If 
    Next 
    If Not openflag Then 
     Set getNewTab = CreateObject("InternetExplorer.Application") 
     getNewTab.Visible = True 
     Exit Function 
    End If 
    Do: WScript.Sleep 100: Loop Until SWObj.Count > cnt 'wait until new tab is opened 
    For Each i In SWObj 
     If LCase(Right(i.FullName, 12)) = "iexplore.exe" Then 
      If Not hasObj(i, objs) Then 
       Set getNewTab = i 
       Exit Function 
      End If 
     End If 
    Next 
End Function 

Function hasObj(obj, col) 
    For Each i In col 
     If obj Is col(i) Then 
      hasObj = True 
      Exit Function 
     End If 
    Next 
End Function 
3

简单的解决方法是刚刚重新分配新的标签对象

IE.navigate "http://www.bbc.co.uk/news/", CLng(2048) 
Do While IE.Busy Or Not IE.readyState = IE_READYSTATE.complete: DoEvents: Loop 
With CreateObject("Shell.Application").Windows 
Set IE = .Item(.Count - 1) 
End With 
0

试一下这个代码:

Sub FillinternetForm() 
    Dim ie As Object 
    Set ie = CreateObject("Internetexplorer.Application") 
    ie.Navigate "https://google.com" 
    ie.Visible = True 
    While ie.Busy 
     DoEvents 'wait until IE is done loading page. 
    Wend 
    ie.Document.all("lst-ib").Value = "Fast Sub" 
    ie.Navigate "https://google.com", CLng(2048) 
    ie.Document.all("lst-ib").Value = "Fast slow Sub" 
End Sub 
+0

第一个标签工作良好,当第二打开它的非填充价值指南关于这个 任何人都可以在这几个代码工作正常吗? – Raulsoul 2016-11-30 13:17:37