PDA

View Full Version : [SOLVED] Get current webpage address



gibbo1715
03-19-2007, 05:40 AM
Hi All

I have IE open and what i want to do is from excel copy the current url into a cell, can this be done, or if not is there a simple VBScript that i can embed in the page that will tell me my current url

thanks

Gibbo

mvidas
03-19-2007, 12:07 PM
Gibbo,
Are you sure there is only going to be one IE window open, and zero explorer windows open? Automating IE can be done easily, however if there are multiple you may not get the one you want, and if you have an explorer windows open that could throw it for a loop. Take a look at http://www.vbaexpress.com/forum/showthread.php?t=11148 for some more information about it all.
Anyways, to show you how you can do this, set a reference to Microsoft Internet Controls in your vba project, then run the following:Sub GetIEWindows()
'Requires reference to Microsoft Internet Controls
Dim SWs As SHDocVw.ShellWindows, vIE As SHDocVw.InternetExplorer

'Establish link to IE application
Set SWs = New SHDocVw.ShellWindows

For Each vIE In SWs
If Left(vIE.LocationURL, 4) = "http" Then 'avoid explorer windows/etc this way
If MsgBox("IE Window found. The URL is:" & vbCrLf & vIE.LocationURL & vbCrLf & _
vbCrLf & "Do you want to see the html?", vbYesNo) = vbYes Then
'Show html in a msgbox
MsgBox vIE.Document.Body.innerHTML
'Or put it to a file
'dim vFF as long
'vff=freefile
'open "C:\thehtml.txt" for output as #vff
'print #vff,vie.document.body.innerhtml
'close #vff
End If
End If
Next

Set SWs = Nothing
Set vIE = Nothing
End SubYou can see tht vIE.LocationURL is the way to get the URL you're looking for, but this shows you how to iterate through all IE windows (if the one you want will follow a certain pattern this should help) and how to avoid explorer windows (as well as get the HTML of the page you're looking at too).
Matt

lucabotola
12-05-2011, 01:02 PM
excellent code

AlexFe
07-17-2017, 05:03 PM
This is GREAT code, but I have a question:


Sub GetIEWindows()
'Requires reference to Microsoft Internet Controls
Dim SWs As SHDocVw.ShellWindows, vIE As SHDocVw.InternetExplorer

'Establish link to IE application
Set SWs = New SHDocVw.ShellWindows

For Each vIE In SWs
If Left(vIE.LocationURL, 4) = "http" Then 'avoid explorer windows/etc this way
If MsgBox("IE Window found. The URL is:" & vbCrLf & vIE.LocationURL & vbCrLf & _
vbCrLf & "Do you want to see the html?", vbYesNo) = vbYes Then
'Show html in a msgbox
MsgBox vIE.Document.Body.innerHTML
'Or put it to a file
'dim vFF as long
'vff=freefile
'open "C:\thehtml.txt" for output as #vff
'print #vff,vie.document.body.innerhtml
'close #vff
End If
End If
Next

Set SWs = Nothing
Set vIE = Nothing
End Sub

I don't want to show a message box showing the url, or pop up a message box showing the underlying html (although really cool). I just want to save the URL as a string or value, and then have the URL (i.e. string or value or whatnot) write for me.

How do I do that?

Thanks!
Alex.

SamT
07-17-2017, 05:20 PM
Dim URLVar As String

URLVar = vIE.LocationURL


I am closing this 13yo thread now.

If you need more help, please start a new thread. Thank you.