none
Automate Internet Explorer running as different user with VBA RRS feed

  • Question

  • My Windows is logged-in as user1. I have to launch Internet Explorer via runas command as user2user1 executes the cmd line: runas /user:user2 "C:\Program Files\Internet Explorer\iexplore.exe". Once IE(user2) is running, with VBA(user1) I am not able to find shell application of IE(user2) with this snippet.

    Set objShell = CreateObject("Shell.Application")
    Set objShellWindows = objShell.Windows
    
    For Each obj In objShellWindows
    
    next obj

    Also I am not able to do this with Windows(user1), IE(user2) and VBA(user2).

    When the IE is launched as user1, this code works perfectly.

    How to manipulate IE(user2) with VBA(user1)? Thx



    • Edited by kabarto1986 Monday, September 9, 2019 9:16 AM
    Monday, September 9, 2019 6:59 AM

All replies

  • When you launch a program as a different user, the program is (understandably) running as that other user.

    The fact that the program is visible in the current logon session doesn't mean it's accessible to you programmatically.

    What problem are you trying to solve?


    -- Bill Stewart [Bill_Stewart]

    Monday, September 9, 2019 2:06 PM
    Moderator
  • I have dedicated server running for ID user1. I am doing the automation of website where users are verified based on their IDs (guessing... Single Sign-On mechanism). There is no chance to obtain credentials for user1. What I have are credentials of user2 who is capable to access the web site. There is any way (workaround) how to force system to control IE programmatically? Or somehow tell to website that user2 is comming...
    • Edited by kabarto1986 Monday, September 9, 2019 7:34 PM
    Monday, September 9, 2019 7:28 PM
  • Normally web-based single-sign on (SSO), in a Windows environment, is accomplished using integrated Windows authentication on the browser side. If you want to authenticate using a different user, start the browser using the other user's account.

    But this is an application architecture question that's not really suitable for a scripting forum.


    -- Bill Stewart [Bill_Stewart]

    Tuesday, September 10, 2019 2:03 PM
    Moderator
  • Use Powershell.

    function wait {
      param([int]$stop = 3)
      Start-Sleep -seconds $stop
    }
    
    function SendWait {
      param([string]$keys)
      [System.Windows.Forms.SendKeys]::SendWait($keys) 
    }
    
    'Launching IE as my test account.'
    $userid = 'admin'
    $pswd = 'password'
    $secpswd = ConvertTo-SecureString $pswd -AsPlainText -Force
    $Cred = New-Object System.Management.Automation.PSCredential ($userid, $secpswd)
    start-process -FilePath 'C:\Program Files\Internet Explorer\iexplore.exe'-ArgumentList 'about:blank' -Credential $cred
    wait 10
    
    'Searching for IE'
    get-process -name iexplore |   foreach {
        "{0} - {1} - {2}" -f $_.Name, $_.id, $_.MainWindowTitle
        if ($_.MainWindowTitle -ne '') {
           $ie = $_
        }
    }
    "I will work with {0}" -f $ie.MainWindowTitle
     
    [void] [System.Reflection.Assembly]::LoadWithPartialName("'Microsoft.VisualBasic")
    [Microsoft.VisualBasic.Interaction]::AppActivate($ie.ID)
    [void] [System.Reflection.Assembly]::LoadWithPartialName("'System.Windows.Forms")
    SendWait("^{HOME}{ESC}{DEL}")
    wait
    SendWait("www.yahoo.com{DEL}")
    wait
    SendWait("{ENTER}")
    
    
           
    

    Tuesday, September 10, 2019 7:55 PM
  • @MotoX80: With your approach am I able to call method like this:
    $ie.document.getelementbyid("select_123").... ? If yes, how exactly?
    Wednesday, September 18, 2019 5:06 AM
  • I don't know. If you are calling document.getelementbyid I believe that you need to use the InternetExplorer.Application object.

    Here's a sample: https://westerndevs.com/simple-powershell-automation-browser-based-tasks/  

    Your initial question shows a createobject for shell.application and you say that you want to manipulate IE, but you don't explain how. I'm sorry you didn't provide enough of your code for us to know what you are doing. 

    Have you tried setting IE to prompt for ID+pswd. I would expect that that would eliminate the need to launch IE as a different user. 

    Or fix the security on the web site to allow user1 to log on. 

    Wednesday, September 18, 2019 11:40 PM
  • Thanks for your answer. The main intention is written in title ...I'd like to automate. Never mind ;D. I tried Zones, but didn't work. To fix the security on web site is not possible. Let's have a look what we have. I can launch IE under different user. You know how to control (automate) IE with power-shell. Your url hint is fine but I am struggling with power shell as newbie. Here is my snippet of VBA working perfectly for user1, Would you please help me to retype my VBA to PowerShell code? Thanks a lot for your help

    sub clickIE()
       Set IE = GetIE("/hotline/", False)
       Set ElementCol=IE.Document.getElementById("lstParametros")
       ElementCol.click 
    end sub
    
    Function GetIE(sLocation As String, closeIE As Boolean) As Object
        Set objShell = CreateObject("Shell.Application")
        Set objShellWindows = objShell.Windows
        For Each o In objShellWindows
              If closeIE Then
                      o.Quit
              Else
                 If o.AddressBar And 0 < InStr(1, o.LocationURL, sLocation, vbTextCompare) Then
                      Set retVal = o
                 End If
              End If
        Next o
        Set GetIE = retVal
    End Function



    • Edited by kabarto1986 Monday, September 23, 2019 1:16 PM
    Monday, September 23, 2019 1:08 PM
  • You cannot access a program running under another users account. This is a fundamental Windows restriction. THe is no way around it.


    \_(ツ)_/

    Monday, September 23, 2019 5:58 PM
  • With jrv's comment in mind, instead of doing a runas for IE, convert your VBA code to a VBS script and run both the script and IE as user1.

    https://support.foxtrotalliance.com/hc/en-us/articles/360023770832-How-To-Automate-IE-With-VBScript

    If your current VBA platform is Excel, have it launch the script and wait. Have the script create a data file that Excel can then read and update the spreadsheet. 

    Monday, September 23, 2019 6:55 PM
  • Fired by double-click (test.vbs) on VBS in environment user1:
    
    File: test.vbs
    Set wshShell = CreateObject("WScript.Shell")
    strRunAsCmd = "runas /user:user2 /savecred ""cscript C:\Automation\_SHREDDER\ASYNC\123.vbs"""
    wshShell.Run strRunAsCmd
    
    
    File: 123.vbs
    Dim objIE
    Set objIE = CreateObject("InternetExplorer.Application")
    With objIE
      .Visible = True 
      .Navigate("http://owa/")    
    End With
    IE has launched but after 2 seconds got frozen...blank page, no interaction even via GUI


    • Edited by kabarto1986 Tuesday, September 24, 2019 10:44 AM
    Tuesday, September 24, 2019 10:43 AM
  • You cannot access a program running under another users account. This is a fundamental Windows restriction. THe is no way around it.


    \_(ツ)_/

    thanks, but how it is possible that this is working well. I can attached to any process (user1, user2...):

    'Searching for IE, works with any user'
    get-process -name iexplore |   foreach {
        "{0} - {1} - {2}" -f $_.Name, $_.id, $_.MainWindowTitle
        if ($_.MainWindowTitle -ne '') {
           $ie = $_
        }
    }

    and this not (I see only user1):

    $app = new-object -com shell.application
    $ie = $app.windows() | where {$_.Type -eq "HTML Document" -and $_.LocationURL -match "/"}
    $ie.navigate("http://www.google.com")

    There is a way how to combine those two snippets?

    Like not create a new object  

    $app = new-object -com shell.application

    but create it from the reference (e.g. Process ID, Title, etc)

    $app =  $_.MainWindowTitle





    • Edited by kabarto1986 Tuesday, September 24, 2019 10:58 AM
    Tuesday, September 24, 2019 10:47 AM
  • As an admin you can read the process properties but you cannot access the data in the process.

    I recommend learning a bit about computer science and systems engineering to understand the difference Between properties of processes and the process and data itself.  A user in windows cannot access the session or data of a process running under another user.


    \_(ツ)_/

    Tuesday, September 24, 2019 11:13 AM
  • IE has launched but after 2 seconds got frozen...blank page, no interaction even via GUI


    I took the example from that web site and added some error checking and msgboxes. Try this. Replace the msgboxes with appropriate error logging code when you are done with your testing.

    'Dim the primary objects.
    
    Dim objIE
    
    msgbox "Say go." 
    'Set the primary objects.
    on error resume next
    Set objIE = CreateObject("InternetExplorer.Application")
    if err.number <> 0 then 
    	msgbox "Unable to Createobject - " & err.number & " - " & err.description
    	wscript.quit
    end if
    
    With objIE
    
      .Visible = True    'Make sure to set the window of IE to visible.
    
      .Navigate("https://www.google.com/")    'Navigate to the desired website.
    	if err.number <> 0 then 
    		msgbox "Unable to navigate - " & err.number & " - " & err.description
    		wscript.quit
    	end if	
    
    
      Do While .Busy Or .readyState <> 4
        'Do nothing, wait for the browser to load.
      Loop
    
      Do While .Document.ReadyState <> "complete"
        'Do nothing, wait for the VBScript to load the document of the website.
      Loop
     
      '***THIS IS WHERE YOU DO SOMETHING***
    
    .Document.getElementsByName("q").Item(0).Value = "Foxtrot RPA" 'Write something to search for.
    .Document.getElementsByName("btnK").Item(0).Click 'Click to search.
    
    if err.number <> 0 then 
    	msgbox "Unable to getElementsByName - " & err.description
    	wscript.quit
    end if
    
    End With 
    
    'Clear the objects.
    
    msgbox "Click ok to end." 
    
    objIE.quit
    
    objIE = Nothing
    
    

    and this not (I see only user1):


    The other processes were not launched by the shell. 

    https://docs.microsoft.com/en-us/windows/win32/shell/shell

    Windows Creates and returns a ShellWindows object. This object represents a collection of all of the open windows that belong to the Shell.


    Tuesday, September 24, 2019 1:35 PM
  • jrv is correct. Due to the fact that you are attempting to access a process running as a different user and/or a different integrity level, your attempts to interact with that process are going to be frustrated because system security is designed to prevent you from doing what you are trying to do (see When people ask for security holes as features).


    -- Bill Stewart [Bill_Stewart]

    Tuesday, September 24, 2019 1:36 PM
    Moderator