none
Internet Explorer Automation with VBA when there are multiple sessions RRS feed

  • Question

  • Hi all.

    Here is my situation. We use a third party web-based database application. We use Internet Explorer 8 to access this application.

    This application allows users to log in under multiple user IDs as long as they start Internet Explorer 8, navigate to the web-based app URL, and then choose File-->New Session in Internet Explorer when they want to log into a second instance of the application under a different user ID. They do this because the application can view several different discrete databases. So the user may need to have two databases open in this application at once, in different Internet Explorer windows. This part works just fine.

    The two instances are easily distinguished because one instance will have "DATABASEX" as part of the URL, and the second instance will have "DATABASEY" as part of its URL. I am using that string in the URL to differentiate between the instances.

    I have written an Access App that, through VBA, opens a URL for this database application in a new window.

    The problem is if I use "Set IE = New InternetExplorer" to open the new Internet Explorer window, it always creates this new Internet Explorer window in the first session the user logged into.

    Here is part of the code I'm using:

    Dim IE As InternetExplorer 
    Dim FoundIEWin1 As SHDocVw.InternetExplorer 
    Dim ONumber As Long 
    Dim FoundURL As String 
    Dim URLToOpen As String 
    Dim flags As Long 
    SystemDB = Me.Controls("System") 
    ONumber = Me.Controls("Order Number") 
    'GetOpenIEByURL is a function that looks for the string 'SystemDB' in the URL of open Internet Explorer windows and returns the InternetExplorer.Application object of that window. 
    Set FoundIEWin1 = GetOpenIEByURL(SystemDB) 
    FoundURL = FoundIEWin1.LocationURL 
    
    URLToOpen = "https://URLToDBApplication.com/UserCodeHere/SystemDBNameHere/frmABCD.aspx?OrderKey=" & ONumber 
    
    Set IE = New InternetExplorer 
    IE.Navigate URLToOpen 
    With IE 
    .Height = 1075 
    .Width = 945 
    .Top = 20 
    .Left = 225 
    .Visible = True 
    .Toolbar = False 
    .StatusBar = True 
    .MenuBar = False 
    End With

    Here is the GetOpenIEByURL function code:

    Function GetOpenIEByURL(ByVal i_URL1 As String) As SHDocVw.InternetExplorer
    
    Dim objShellWindows As New SHDocVw.ShellWindows
    
    'ignore errors when accessing the document property
    On Error Resume Next
      'loop over all Shell-Windows
      For Each GetOpenIEByURL In objShellWindows
        'if the document is of type HTMLDocument, it is an IE window
        If TypeName(GetOpenIEByURL.Document) = "HTMLDocument" Then
          'check the URL
          If GetOpenIEByURL.Document.URL Like "*" & i_URL1 & "*" Then
            'leave, we found the right window
            Exit Function
          End If
        End If
      Next
    End Function

    So I'm able to find the correct open IE window.

    What I am not able to do is open a new window in the found window's security context.

    Using Set IE to New InternetExplorer creates the new window, but often in the wrong security session, so then when I navigate to the URL using .navigate, the web-based database application throws an "incorrect session" error.

    In other words if you are navigating to a URL with SystemDB = 'DATABASEX' in it, then that URL must to be opened in the Internet Explorer session for 'DATABASEX'. Finding that base InternetExplorer window/object with the GetOpenIEByURL function is no problem, that part works fine. I just can't see how to then open a new window in that base window's security/session context.

    It seems like since I can grab the correct InternetExplorer object with GetOpenIEByURL, that I should somehow then be able to "spawn" a new window from that object as well (which would hopefully therefore be in that window's security session), but I've not yet found any way to do so.

    I've tried opening the new URL in the base window using the navOpenInNewWindow flag in the .Navigate arguments, however I have tabbed browsing turned on, and it always opens in a new TAB instead of a new window. I need this the URL to open in its own separate window which is part of the correct Internet Explorer session.

    I've been banging my head on this one for a while. It seems like there has to be a way to deal with this problem.

    I'm wondering if somehow programmatically accessing the session cookies would work, but have no idea how to do that either.

    Any ideas would be super appreciated.



    Monday, February 13, 2012 4:20 PM

All replies

  • What about not creating a New window, but continue in the same window using the GetObject instead. Does that work?

    Something like this I have in mind:

    Set IE = GetObject(,"InternetExplorer") 
    IE.Navigate URLToOpen 
    With IE 
    .Height = 1075 
    .Width = 945 
    .Top = 20 
    .Left = 225 
    .Visible = True 
    .Toolbar = False 
    .StatusBar = True 
    .MenuBar = False 
    End With

    Hope this helps,


    Daniel van den Berg | Washington, USA | "Anticipate the difficult by managing the easy"

    Monday, February 13, 2012 5:16 PM
    Moderator
  • Hi Daniel.

    Thanks for the thought.
    However, I am already able to get the object I need using the GetOpenIEByURL function.
    That function searches the URLs of any open IE windows for the string passed to it, and returns the object for the window I need.

    I just cannot figure out how to open a new window that is "bound" to the same IE session as the returned object.

    The main problem here is that there are two separate IE8 sessions going for this web-based database application.
    One session is logged into a database with the name "DATABASEX", the other is logged into a database with the name "DATABASEY".
    The way this web-based application works, you can tell which database it is displaying just from looking at the URL of its Internet Explorer window.
    That URL contains the name of the database from which it is displaying information.

    When I send a URL to open in IE, it will contain one of those two database name strings. (DATABASEX or DATABASEY)

    Basically, the problem is that the opened URL needs to open in a "new" Internet Explorer popup window, but be part of a specific existing Internet Explorer session depending upon which database the URL call is aimed at.

    If the URL contains the string "/DATABASEX/", then it needs to open within the Internet Explorer session for DATABASEX.
    If the URL contains the string "/DATABASEY/", then it needs to open within the Internet Explorer session for DATABASEY. 

    I cannot figure out how to open the URL in the IE session that matches the database name.

    There must be a way to do this, but I'm beginning to suspect it involves some windows api calls I am not familiar with.

    Monday, February 13, 2012 6:15 PM
  • Hi

    You can use the "Harness" library to automatic operation for the Internet Explorer.

    see http://harness.codeplex.com
    Wednesday, March 28, 2012 5:53 PM