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
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
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:[vba]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[/vba]You 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
excellent code
This is GREAT code, but I have a question:
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.
I am closing this 13yo thread now.Dim URLVar As String URLVar = vIE.LocationURL
If you need more help, please start a new thread. Thank you.
I expect the student to do their homework and find all the errrors I leeve in.
Please take the time to read the Forum FAQ