There are only two FoxPro functions you need to think about to retrieve another application's objects. Once you have obtained the object, you can immediately begin to access properties and methods of the object.
This command creates an object from a class definition or an OLE object. It is also used to create objects of built in FoxPro or user-defined classes. Because this session is about creating OLE objects, I will intentionally leave out the syntax and descriptions of creating other types of classes to avoid confusion. For consistency with Microsoft documentation, much of this information in this section is derived directly from FoxPro's help file.
Syntax:
<memvar> = CREATEOBJECT(cClassName)
cClassName: Specifies the OLE object from which the new object is created.
FoxPro will search for the class name you specify in the following order:
1. Microsoft Visual FoxPro™ base classes.
2. User-defined class definitions in memory in the order they were loaded.
3. Classes in the current program.
4. Classes in .VCX class libraries opened with SET CLASSLIB.
5. Classes in procedure files opened with SET PROCEDURE.
6. Classes in the Visual FoxPro program execution chain.
7. The Windows Registry (for OLE objects).
OLE objects are created using the following syntax for cClassName:
ApplicationName.Class
For example, to create a Microsoft Excel object using OLE Automation, you can use the following syntax:
xl = CREATEOBJECT("Excel.Application")
When this code is run, Microsoft Excel is started but it is not visible. You will not see Microsoft Excel if you ALT+TAB through all your running applications, nor will you see it if you bring up the Task Manager with CTRL+ESC. Note that if Microsoft Excel is already running, another separate instance will be created when you execute this command. Also note that Microsoft Word functions a little differently, as will be explained in the "FoxPro 3.0 OLE Automation" section.
This command activates an OLE automation object and creates a reference to the object.
Syntax:
GETOBJECT([cFileName [, cClassName]])
cFileName: Specifies the full path and name of the file to activate. The application does not need to be specified, because the OLE dynamic link libraries determine the application to start based on the file name you provide.
For example, the following code launches Microsoft Excel, opens a file named BUDGET.XLS, and creates a reference through an object memory variable named xlBudget:
xlBudget = GETOBJECT("C:\EXCEL\WORK\BUDGET.XLS")
cClassName: Specifies the class name of the object to retrieve. Some applications can store more than one object type in the same file, allowing you to use the class name to specify the object to activate. For example, if a word processing application stores its documents, macro definitions, and ToolBar objects in the same file, you can create a reference to the document file with the following command:
mDocFile = GETOBJECT("C:\WRDPROC\MYDOC.DOC", "WrdProc.Document")
If you specify the empty string ("") for cFileName, GETOBJECT( ) creates a reference to the currently active object of the class you include. For example, if Microsoft Excel is running, you can create a reference to it with the following command:
xl = GETOBJECT("", "Excel.Application")
If Microsoft Excel is not running, a trappable error will be generated. Trapping the error and resorting to the CREATEOBJECT() function is a common technique to test if an application is already running. An alternative technique is to use dynamic data exchange (DDE) to establish a link with the server application on the System topic, which all DDE applications support. If the link is successfully established, the application is running. This concept is covered in more detail in the "FoxPro 3.0 OLE Automation" section.