2012-06-22 18 views
6

Sto lavorando con l'oggetto di Internet Explorer in Visual Basic. C'è un modo per copiare l'URL corrente che IE sta visualizzando, quindi posso incollarlo altrove con i miei appunti?Ottieni l'URL corrente in IE utilizzando Visual Basic

+0

questo forse è un po 'più complesso di quello che vuoi, ed è VB.NET, ma dovrebbe essere traducibile. Spero che aiuti [Ottieni URL] (http://www.codeproject.com/Articles/204929/Getting-current-browser-URL-with-VB-NET) –

+0

Quale applicazione? Excel, Access ..? – Fionnuala

+0

In che modo sei connesso con l'IE corrente? Questo è importante come cosa succede se ci sono più di 1 finestre IE aperte. Sei vincolante con IE in base alla didascalia? Mostrandoci più codice sarebbe davvero d'aiuto. Se si desidera che il codice ottenga l'URL da tutte le finestre di IE, è possibile impostare un riferimento a Microsoft Internet Controls e quindi è possibile eseguire il ciclo di tutte le finestre di IE utilizzando 'ShellWindows'. Una volta ottenuta la finestra, usa semplicemente '.LocationURL' per ottenere l'indirizzo. –

risposta

6

Supponendo di avere già la finestra di IE identificato, che di per sé è un po 'più complicato - posso approfondire questo, se necessario:

Dim ieIEWindow As SHDocVw.InternetExplorer 
Dim sIEURL As String 

'Set your IE Window 

sIEURL = ieIEWindow.LocationURL 

Per ottenere la finestra di IE, è necessario fare riferimento la libreria Microsoft Internet Controls (ieframe.dll) nell'editor VBA andando a Tools =>References... e selezionandolo dall'elenco. Se quell'elemento non è disponibile, il file .dll per me si trova a C:\Windows\System32\ieframe.dll.

Una volta impostato il riferimento, si avrà accesso alla cosiddetta libreria SHDocVw nel codice.

È possibile afferrare la finestra di IE (supponendo unica aperta) utilizzando le seguenti (non testato, modificato/ridotto dal mio codice di lavoro):

Public Function GrabIEWindow() As SHDocView.InternetExplorer 

Dim swShellWindows As New SHDocVw.ShellWindows 
Dim ieOpenIEWindow As SHDocVw.InternetExplorer 

    Set GrabIEWindow = Nothing 

    ' Look at the URLs of any active Explorer windows 
    ' (this includes WINDOWS windows, not just IE) 
    For Each ieOpenIEWindow In objShellWindows 

     ' Check the I.E. window to see if it's pointed 
     ' to a web location (http) 
     If Left$(ieOpenIEWindow.LocationURL, 4) = "http" Then 
      ' If so, set this window as the one to use. 
      ' This will need to be modified to create 
      ' a list if you want to select from more 
      ' than one open window 

      ' Optional grab the HWND for later reference... 
      Dim lWindowID As Long 
      lWindowID = ieOpenIEWindow.HWND 

      Set GrabIEWindow = ieOpenIEWindow 

      Exit Function 
     End If 

    Next OpenIEWindow 

End Function 

Quanto sopra può anche essere modificato per consentire una selezione di più finestre aperte di IE.

+0

+ 1 Yup Se è presente solo una finestra aperta o se la finestra IE è identificata. –

+0

@SiddharthRout Io uso questo tipo di automazione su base giornaliera e ho sviluppato alcuni strumenti sporchi ma efficaci per realizzare questo. (modulo che elenca tutte le finestre disponibili, seleziona l'utente, la sessione quindi si collega all'HWND, in alcuni casi assicurando che la finestra sia ancora aperta, ecc.) – Gaffi

+0

Kool :) Potresti modificare il tuo post e aggiungere che l'utente deve aggiungere riferimento a Microsoft Internet Controls? –

2

Dannazione! Questo mi ricorda i miei giorni vb6 :)

Ok ecco quello che ho. Se ci sono più di 1 Windows IE allora prenderà l'ultimo attivo (Current) IE window else se c'è solo una finestra quindi ci vorrebbe.

'~~> Set a reference to Microsoft Internet Controls 

'~~> The GetWindow function retrieves the handle of a window that has 
'~~> the specified relationship (Z order or owner) to the specified window. 
Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, _ 
ByVal wCmd As Long) As Long 

'~~> The GetForegroundWindow function returns the handle of the foreground 
'~~> window (the window with which the user is currently working). 
Private Declare Function GetForegroundWindow Lib "user32"() As Long 

Sub GetURL() 
    Dim sw As SHDocVw.ShellWindows 
    Dim objIE As SHDocVw.InternetExplorer 
    Dim topHwnd As Long, nextHwnd As Long 
    Dim sURL As String, hwnds As String 

    Set sw = New SHDocVw.ShellWindows 

    '~~> Check the number of IE Windows Opened 
    '~~> If more than 1 
    hwnds = "|" 
    If sw.Count > 1 Then 
     '~~> Create a string of hwnds of all IE windows 
     For Each objIE In sw 
      hwnds = hwnds & objIE.hwnd & "|" 
     Next 

     '~~> Get handle of handle of the foreground window 
     nextHwnd = GetForegroundWindow 

     '~~> Check for the 1st IE window after foreground window 
     Do While nextHwnd > 0 
      nextHwnd = GetWindow(nextHwnd, 2&) 
      If InStr(hwnds, "|" & nextHwnd & "|") > 0 Then 
       topHwnd = nextHwnd 
       Exit Do 
      End If 
     Loop 

     '~~> Get the URL from the relevant IE window 
     For Each objIE In sw 
      If objIE.hwnd = topHwnd Then 
       sURL = objIE.LocationURL 
       Exit For 
      End If 
     Next 
    '~~> If only 1 was found 
    Else 
     For Each objIE In sw 
      sURL = objIE.LocationURL 
     Next 
    End If 

    Debug.Print sURL 

    Set sw = Nothing: Set objIE = Nothing 
End Sub 

NOTA: non ho fatto alcuna gestione degli errori. Sono sicuro che puoi occupartene;)

+0

Questo è un po 'diverso dai miei metodi! Uso alcuni riferimenti alle DLL esterne, ma solo per manipolare le pagine (ad esempio, salta avanti e indietro tra più). Come gestite 'sw.Count = 1' quando l'unica finestra ha un percorso non http, dal momento che una finestra può essere una cornice standard di Windows (ad esempio' My Computer')? – Gaffi

+0

Come ho già detto, non ho incluso alcuna gestione degli errori. usando 'Left $ (ieOpenIEWindow.LocationURL, 4) = "http" 'come hai fatto, sarebbe stato il mio prossimo passo, ma stavo testando con 3 finestre IE valide con indirizzi reali. :) Fai un tentativo con più di 1 finestra di IE e vedi quale URL ottieni? –

Problemi correlati