Consulting

Results 1 to 5 of 5

Thread: Get current webpage address

  1. #1

    Get current webpage address

    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

  2. #2
    Knowledge Base Approver
    The King of Overkill!
    VBAX Master
    Joined
    Jul 2004
    Location
    Rochester, NY
    Posts
    1,727
    Location
    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 Sub 
    
    
    Formatting tags added by mark007
    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

  3. #3

  4. #4

    Great Code

    This is GREAT code, but I have a question:

    Quote Originally Posted by mvidas View Post
    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 
    
    
    Formatting tags added by mark007
    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.

  5. #5
    Moderator VBAX Wizard SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    5,985
    Location
    Dim URLVar As String 
     
    URLVar =  vIE.LocationURL 
    
    
    Formatting tags added by mark007
    I am closing this 13yo thread now.

    If you need more help, please start a new thread. Thank you.
    Please take the time to read the Forum FAQ

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •