2013-05-13 15 views
5

Utilizzando VBA posso creare un oggetto InternetExplorer con 3 diverse schede utilizzando la seguenteAccesso schede di IE una volta creati

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 

Come posso quindi accedere a queste schede per ....

  1. Quit/Chiudi una scheda specifica?
  2. Passare a un nuovo URL in una scheda specifica?
  3. Accedi a una scheda specifica DOM?

So come fare tutto questo con una sola scheda ma non più schede?

risposta

4

Ho trovato una risposta che sembra funzionare, anche se non ho eseguito test rigorosi. Ho modificato il tuo codice in modo che possa essere eseguito senza il tuo modulo clsIE. Questo codice naviga le schede a 3 url quando vengono aperti e quindi li indirizza a nuovi URL usando l'oggetto shellwindows.

Ho modificato il do mentre linee .busy .. perché non funzionavano per la 2a e 3a scheda in quanto lo stato dell'applicazione IE era pronto mentre le nuove schede stavano ancora caricando.

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 

Utilizza ShellWindows per manipolare le schede. Questo è fatto solo per numero quindi immagino che potrebbe essere soggetto a errori.

Per renderlo più robusto, è possibile che si desideri ottenere alcune informazioni dalla scheda dopo un'operazione e verificare che il valore sia lo stesso al suo ritorno. Ad esempio, è possibile utilizzare sPageTitle = SWs.Item(IETab3Number - 1).Document.Title per memorizzare il titolo della pagina in una scheda, la volta successiva che si desidera utilizzare la scheda è possibile verificare che non sia stata modificata.

+1

Would 5 se potessi :) – user3357963

1

Ecco come ho risolto il problema di raggruppare le schede in una finestra di IE su VBScript./tabGroupS/è smth. come "http://stackoverflow.com". Se la funzione trova la finestra IE con la scheda Stackoverflow aperta, aggiunge una nuova scheda a questa finestra e restituisce questa scheda altrimenti apre una nuova finestra.

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

soluzione semplice è solo riassegnare la nuova scheda per l'oggetto

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

provare questo codice:

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

prima scheda funziona bene quando il secondo apri la sua guida ai valori non di riempimento su questo Qualcuno può inserire questo codice che funziona correttamente? – Raulsoul

Problemi correlati