vbscript how to capture URL from location box in Internet Explorer

2.8k views Asked by At

In a recent question, vbscript using InStr to find info that varies within a URL, I had asked about finding info in a URL. I later realized I didn't need to find the info in the URL, I just needed to be able to capture the URL from the location box in Internet Explorer and be able to use it to gather data from a web page. Here's what I have so far:

Option Explicit
Dim objIE, objShell, objShellWindows
Dim strIDNum, strURL, strWindow, strURLFound, WShell, i

'=============================================================

'===  Code for capturing URL of current page will go here  ===

'=============================================================

strURL = 'URL that is captured by the above coding
strWindow = "Workflow Process"
Set objIE = CreateObject("InternetExplorer.Application")
Set objShell = CreateObject("Shell.Application")
Set objShellWindows = objShell.Windows
Set WShell = CreateObject("WScript.Shell")

strURLFound = False

'To fix item not found error
For Each objIE in objShellWindows
Next

For i = 0 to objShellWindows.Count - 1
    Set objIE = objShellWindows.Item(i)
On Error Resume Next
    If InStr(Ucase(objShellWindows.Item(i).LocationURL), Ucase(strURL)) Then
        If InStr(Ucase(objShellWindows.Item(i).FullName), "IEXPLORE.EXE") Then
            If Err.Number = 0 Then
                If InStr(objShellWindows.Item(i).document.title, (strWindow)) Then
                    strURLFound = True
                    Exit For
                End If
            End If
        End If
    End If
Next

Once I have the URL for the site my users will be on, I will be using the code below to gather the info:

WShell.AppActivate strWindow

WScript.Sleep 300

strIDNum = objIE.document.getElementByID("ID_PlaceHolder").value

How do I go about getting the URL from the page they are on?

2

There are 2 answers

0
Lou On BEST ANSWER

I was able to figure out how to get information from another web site with a URL that changes. First, I needed the base URL to at least get me there, once found, it really didn't matter what additional info was in the URL because each page is set up the same, just different data flowing through it. The end result is the code below.

Option Explicit
Dim objIE, objShell, objShellWindows
Dim strIDNum, strURL, strWindow, strURLFound, WShell, i

strURL = "http://www.myworkplace.com"
strWindow = "Workflow Process"
Set objIE = CreateObject("InternetExplorer.Application")
Set objShell = CreateObject("Shell.Application")
Set objShellWindows = objShell.Windows
Set WShell = CreateObject("WScript.Shell")

strURLFound = False

'To fix item not found error
For Each objIE in objShellWindows
Next

For i = 0 to objShellWindows.Count - 1
    Set objIE = objShellWindows.Item(i)
On Error Resume Next
    If InStr(Ucase(objShellWindows.Item(i).LocationURL), Ucase(strURL)) Then
        If InStr(Ucase(objShellWindows.Item(i).FullName), "IEXPLORE.EXE") Then
            If Err.Number = 0 Then
                If InStr(objShellWindows.Item(i).document.title, (strWindow)) Then
                    strURLFound = True
                    Exit For
                End If
            End If
        End If
    End If
Next

WShell.AppActivate strWindow

WScript.Sleep 300

strIDNum = objIE.document.getElementByID("ID_PlaceHolder").innertext

As you can see in the last line, I also used innertext rather than value to get the ID number.

4
rory.ap On

Have you tried objIE.LocationURL? Also, thanks to Tomalak for providing objIE.document.location.href.