RegisterLog In/Log OutView Cart
O'Reilly Ron's VB Forum Ron's VB Forum
BooksSafari BookshelfConferencesO'Reilly NetworkO'Reilly GearLearning Lab
 


Traveling to
a tech show?

Hotel Search
Hotel Discounts
Discount Hotels
Chicago Hotels
Canada Hotels
California Hotels
Hotels




Date: August 2000
From: Sunil Budhrani
To: ron@oreilly.com
Subject: ProgID of an Active Window

Hi Ron,

Is there a way to know the ProgID of a currently active window?

I know that GetForegroundWindow can return us the Handle of an active window and GetClassName can return us the class name. But I was not able to associate the Class Name returned from GetClassName with the Actual Window. So I am looking for the ProgID.

Please help.

Thanx,
Sunil


Hi Sunil,

There is no way to determine the ProgID, or programmatic identifier, of an active window.

To understand why, let's start by clarifying what a programmatic identifier, or ProgID, represents: It is a string identifier of an externally creatable object; that is, it identifies an exposed object that can be created from outside the application of which it is a part. If you're using early binding in Visual Basic (in other words, if you add a reference to a particular object model to a VB project), objects with ProgIDs generally correspond to classes that allow the use of the New keyword to instantiate new object instances. In short, a ProgID provides an "entry point" into an object model.

The definition suggests that getting the ProgID of the currently active window is somewhat like mixing apples and oranges: there's not necessarily a correspondence between a window and a ProgID. Moreover, when we consider what a window is--namely, that it represents a kind of container for presenting an application's user interface and/or for holding a document or collection of data--it seems likely that, at least in most object models, windows are created internally by the application, and it makes little sense to allow new Window objects to be instantiated from outside of the application.

The Word object model provides a good illustration of this, although most of the other Office object models are similar. The two major creatable objects in the model are the Application object and the Document object. In Word, windows are containers for documents. So if you want to retrieve a reference to a Word window, you first have to instantiate the Word Application object and create a new document using the Add method of the Documents collection or open an existing document using the Open method of the Documents collection. You can then retrieve a reference to a Window object of a particular document. For example:

Dim oWord As Word.Application
Dim oDoc As Word.Document
Dim oWin As Word.Window

Set oWord = New Word.Application
oWord.Visible = True

' create new document
Set oDoc = oWord.Documents.Add()
Set oWin = oDoc.Windows(1)

An alternate version involves creating a new document, which automatically launches Word:

Dim oDoc As Word.Document
Dim oWin As Word.Window

Set oDoc = New Word.Document
Set oWin = oDoc.Windows(1)
oDoc.Application.Visible = True

Note that in either case, a reference to a Window object is obtained from the Document object for which the window serves as a container.

One final comment on windows and ProgIDs. Since you can instantiate a Word Window object using the fully qualified string Word.Window, you might think that this is a Word window object's programmatic identifier. However, it is not. ProgIDs are defined in the registry as direct subkeys of HKEY_CLASSES_ROOT. If you search the registry for a subkey named Word.Windows, you won't find one. The string simply specifies the particular type library (i.e., Word) in which a particular class (i.e., Window) is defined.

--Ron

Return to: Ron's VB Forum



O'Reilly Home | Privacy Policy

© 2007 O'Reilly Media, Inc.
Website: | Customer Service: | Book issues:

All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.