Get current url in IE using Visual Basic

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

+6
source share
2 answers

Assuming you already have an IE window defined, which in itself is a bit more complicated - I can elaborate on this if necessary:

Dim ieIEWindow As SHDocVw.InternetExplorer Dim sIEURL As String 'Set your IE Window sIEURL = ieIEWindow.LocationURL 

To get the IE window, you will 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 this item is not available, the .dll file for me is located in C:\Windows\System32\ieframe.dll .

Once the link is installed, you will get access to the so-called SHDocVw library in your code.

You can capture the IE window (assuming only one is open) using the following (unverified, 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 IE window to see if it 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 select multiple open IE windows.

+6
source

Heck! It reminds me of my vb6 days :)

Ok, thatโ€™s what I have. If there are more than 1 IE window, then it will accept the last active ( Current ) IE window, if there is only one window, then it will take.

 '~~> 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 did not perform error handling. I'm sure you can take care of this;)

+2
source

Source: https://habr.com/ru/post/918763/


All Articles