Logo

Hacker’s Guide to Visual FoxPro
An irreverent look at how Visual FoxPro really works. Tells you the inside scoop on every command, function, property, event and method of Visual FoxPro.

GetObject()

GetObject() opens an existing document or creates a new object handle to an Automation server application. Like many aspects of ActiveX, exactly how this is implemented and the necessary parameters for successful execution vary by application.

Usage

oObject = GetObject( cObjectReference [, cClassName ] )

Parameter

Value

Meaning

cObjectReference

Character, Filename

Name of an existing file, or (depending on the application) a blank or null value.

Character, Moniker

The identifier of an IMoniker interface of a COM object.

Omitted

Return a handle to an existing application, for some Automation servers.

cClassName

Character

Name of a registered server class (you can look them up in the Registry).

Omitting the first parameter and passing “Excel.Application” for the second starts Excel if it is not running, or grabs another handle to the application if it is executing. We use this technique, followed by Excel’s Workbooks.Open method, if we need to work with multiple spreadsheets.

The last several generations of Office applications from Microsoft (since Office 97) are much better behaved than their earlier counterparts. Word, Excel and PowerPoint all behave quite well as Automation servers. However, you should pass only one of the two parameters to this function. Either pass the name, as in GETOBJECT(“MyWorkbook.xls”), or the class name, as in GETOBJECT( , “Excel.Application”). Don’t pass both, unless you have a good reason, such as a nonstandard extension, as in GETOBJECT(“MyDocument.001”, “Word.Application”).

VFP 7 adds the ability to specify a moniker. The Help file says, “For details about COM monikers, search for “IMoniker” on the Microsoft Developer Network.” Do yourself a favor and skip the search for IMoniker on MSDN, unless you understand a lot about COM internals—searching for “moniker” gives a more palatable series of documents, though still providing quite a bit more detail than you may care to see.

So what is a moniker? A moniker is simply a ten-dollar term meaning “a name for another object,” and is the underpinnings of COM. It is an object with a number of methods for finding and binding to the data. For example, monikers are used in a Word document where an Excel spreadsheet has been linked; the moniker is the object that describes where to find the Excel spreadsheet. How do you get a moniker? There are a number of ways: you can call APIs to create monikers (you can create file, item, generic composite, pointer, anti, URL, and class monikers with the API), you can have a pointer to a custom control that implements an IMoniker interface, or you create them with OLE operations. We consider this an expert-level feature.

Example

* Open the MySheet spreadsheet in Excel and get
* a handle to the application.
oExcel1 = GetObject("MySheet.XLS")
* Talk to the running Word application.
oWord = GetObject(, "Word.Application")

See Also

CreateObject()