VBScript - How do I create a new sub that focuses on an existing IE window WITHOUT creating a new object?

107 Views Asked by At

I am trying to create an automation tool for users on a web-based system that have to login. I am not able to use CreateObject because that opens a new IE window which makes the user sign-in again which then kicks them out of the current session.

How do I rewrite the following so it targets the existing IE window and clicks on a button without using CreateObject?

Sub exampleOne
    Dim ie, doc
    set ie = 'cannot use CreateObject - what else can I do? 
    set doc = ie.document
    wshell.AppActivate("Sample Title")      
    doc.getElementByID("button").Click
End Sub
1

There are 1 best solutions below

3
Deepak-MSFT On

I suggest you try to refer to the example below may help you to focus existing IE window and click the button on a webpage.

Sample VBScript:

Set shell = WScript.CreateObject("WScript.shell")

Dim objInstances, objIE, IE
Set objInstances = WScript.CreateObject("Shell.Application").windows
If objInstances.Count > 0 Then 
    For Each objIE In objInstances
        If InStr(objIE.LocationURL,"https://Your_site_URL_here...") > 0 then  '/// Modify the URL on this line...
        Set IE = objIE
        End if
Next
End If
shell.AppActivate ("Internet Explorer")

Wait IE, 2000
IE.document.getElementById("btn1").Click

Wait IE, 2000

'IE.Quit

Sub Wait(IE, ms)
    Do
        WScript.Sleep ms
    Loop While IE.ReadyState < 4 And IE.Busy
End Sub

Further, you can try to understand the code example try to modify it based on your own requirements.

Thanks for your understanding.