7

I am working with the Internet Explorer object in Visual Basic. Is there a way to copy the current URL IE is displaying so I can paste it elsewhere with my clipboard?

3
  • this maybe a bit more complex than you want, and it's VB.NET, but it should be translatable. Hope it helps Get URL Commented Jun 22, 2012 at 14:45
  • Which application? Excel, Access ..? Commented Jun 22, 2012 at 14:48
  • How have you connected with the current IE? This is important as what if there are more than 1 IE windows open. Are you binding with IE based on the caption? Showing us more code would actually help. If you want the code to get the URL from all IE windows then you can set a reference to Microsoft Internet Controls and then you can loop through all IE windows using ShellWindows. Once you get the window, simply use the .LocationURL to get the address. Commented Jun 22, 2012 at 15:04

2 Answers 2

8

Assuming you already have the IE window identified, which itself is a little more complicated - I can elaborate on this if needed:

Dim ieIEWindow As SHDocVw.InternetExplorer
Dim sIEURL As String

'Set your IE Window

sIEURL = ieIEWindow.LocationURL

To get the IE window, you'll need to reference the Microsoft Internet Controls library (ieframe.dll) in the VBA editor by going to Tools=>References... and selecting it from the list. If that item is not available, the .dll file for me is located at C:\Windows\System32\ieframe.dll.

Once the reference is set, you'll have access to the so-called SHDocVw library in your code.

You can grab the IE window (assuming only one open) using the following (untested, modified/reduced from my own working code):

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

The above can also be modified to allow for a selection of multiple open IE windows.

Sign up to request clarification or add additional context in comments.

4 Comments

+ 1 Yup If there is only one window open or if the IE Window is identified.
@SiddharthRout I do use this kind of automation on a daily basis and have developed some dirty but effective tools for accomplishing this. (form that lists all available windows, user selects, session then binds to the HWND, in some cases ensuring the window is still open, etc.)
Kool :) You might want to amend your post and add that the user needs to add reference to Microsoft Internet Controls?
@SiddharthRout Perhaps, but again, I was assuming they already had the window identified, which means they should already have that... Adding now.
2

Damn! This reminds me of my vb6 days :)

Ok here is what I have. If there are more than 1 IE windows then it will take the last active (Current) IE window else if there is only one window then it would take that.

'~~> 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

NOTE: I have not done any error handling. I am sure you can take care of that ;)

2 Comments

This is quite a bit different than my methods! I use some of the external dll references, but only to manipulate the pages (i.e. jump back and forth between multiple). How do you handle sw.Count = 1 when the only window has a non-http path, since a window may be a standard Windows frame (i.e. My Computer)?
Like i mentioned I have not included any error handling. using Left$(ieOpenIEWindow.LocationURL, 4) = "http" like you have done would have been my next step but I was testing with 3 valid IE windows with actual addresses. :) Give it a try with more than 1 IE window and see what URL do you get?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.