'CreateObject function for "Shell.Application" vs "InternetExplorer.Application"

In an answer to this post VBA Macro For Already Open IE Window CreateObject() is used twice, once with "Shell.Application" and another time with "InternetExplorer.Application".

The first time it is used to reference an already open or already active window.

The second time it is used to create a new browser window.

Sub GetIE_LateBinding()

    Dim IE As Object

    With CreateObject("Shell.Application").Windows

        If .Count > 0 Then
            ' Get IE
            Set IE = .Item(0) ' or .Item(.Count - 1)
        Else
            ' Create IE
            Set IE = CreateObject("InternetExplorer.Application")
            IE.Visible = True
        End If

        IE.Navigate "http://support.microsoft.com/kb/q176792/"

        Set IE = Nothing

    End With

End Sub

I tried the following code. It results in a new IE window each time even when there is already an instance open.

Public Sub Trial()
    Dim IE As Object
    Set IE = CreateObject("InternetExplorer.Application")
    IE.Visible = True
End Sub

I can understand the behavior of "InternetExplorer.Application" - creating new IE application instance each time hence new window. But "Shell.Application" seems to fetch already open shell application instance and hence, we are able to browse the already open IE windows. Seems a bit confusing. What am I missing?

Note: I am guessing something related to class_initialize() for "Shell.Application".

vba


Solution 1:[1]

IE and Explorer used to be the same program which is why Shell.Application lists open IE Windows. It is only doing this because they used to both be Explorer windows. You are lucky this meets your needs.

CreateObject is used to create a new object.

GetObject(filename) connects to an open file, and if not open, opens it.

Set xlBook = GetObject("C:\Users\David Candy\Documents\Super.xls")
msgbox xlbook.name

GetObject("","shell.application") connect to an existing object and fails if it's not running.

Set GetExcelApp = GetObject("", "Excel.Application")
Msgbox GetExcelApp

Don't get too hung up on objects. Many objects are just function libraries like Shell.Application. You created a new function library object.

From COM help.

 **Component Automation**  

Mapping Visual Basic to Automation

Visual Basic provides full support for Automation. The following table lists how Visual Basic statements translate into OLE APIs.

Visual Basic statement OLE APIs

CreateObject (ProgID)

CLSIDFromProgID CoCreateInstance QueryInterface to get IDispatch interface.

GetObject (filename, ProgID)

CLSIDFromProgID CoCreateInstance QueryInterface for IPersistFile interface. Load on IPersistFile interface. QueryInterface to get IDispatch interface.

 GetObject (filename)

CreateBindCtx creates the bind context for the subsequent functions. MkParseDisplayName returns a moniker handle for BindMoniker. BindMoniker returns a pointer to the IDispatch interface. Release on moniker handle. Release on context.

GetObject (ProgID)

CLSIDFromProgID GetActiveObject on class ID. QueryInterface to get IDispatch interface.

Dim x As New interface

Find CLSID for interface. CoCreateInstance QueryInterface

© Microsoft Corporation. All rights reserved.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1